Click here to Skip to main content
15,900,818 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralMemory Management Pin
Stew10-Aug-02 6:21
Stew10-Aug-02 6:21 
GeneralRe: Memory Management Pin
Maximilien10-Aug-02 6:30
Maximilien10-Aug-02 6:30 
GeneralRe: Memory Management Pin
Todd Smith11-Aug-02 8:15
Todd Smith11-Aug-02 8:15 
GeneralThreads Pin
AJ12310-Aug-02 6:00
AJ12310-Aug-02 6:00 
GeneralRe: Threads Pin
Paul M Watt10-Aug-02 6:19
mentorPaul M Watt10-Aug-02 6:19 
GeneralRe: Threads Pin
AJ12310-Aug-02 6:29
AJ12310-Aug-02 6:29 
GeneralRe: Threads Pin
Daniel Lohmann11-Aug-02 6:52
Daniel Lohmann11-Aug-02 6:52 
GeneralRe: Threads Pin
Wes Jones12-Aug-02 14:34
Wes Jones12-Aug-02 14:34 
The best solution is to use the MsgWaitForMultipleObjects API.
Also, as someone pointed out, don't use SendMessage in your worker thread when you want to send a message to the main thread, use PostMessage.

It'll look something like this:
HANDLE hThread = //the handle to the thread that you started
DWORD dwWait;
do{
   dwWait  = MsgWaitForMultipleObjects(1,
                                           &hThread,
                                           FALSE,
                                           5000,
                                           QS_ALLINPUT);
   switch( dwWait )
   {
   case WAIT_OBJECT_0:
       //handle became signalled!
                      //this means your thread is done!
       break;
   case WAIT_OBJECT_0 + 1:
       {
       //This thread awoke due to sent/posted message
       //process the message Q
       //
       //  There is a message in this thread's queue, so
       //  MsgWaitForMultipleObjects returned.
       //  Process those messages, and wait again.

       MSG msg;
       while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) )
       {
           TranslateMessage(&msg);
           DispatchMessage(&msg);
       }
          }break;
   case WAIT_TIMEOUT:
       {
           // some sort of problem occurred
       }break;
       default:
               {
                  //unexpected return value from MsgWaitForMultipleObjects
               }break;
       }//end switch(dwWait)
 }while( dwWait != WAIT_OBJECT_0 );


 //when your code get's here, it's because the worker thread has finished


The above code snippet assumes that your worker thread exits when it's finished doing it's thing. If it hangs around in a wait state for future requests, it's best to create an event object which is signalled when the worker thread finishes it's task & then have the above code snippet wait on the event handle instead of the thread handle.

FYI, the thread handle becomes 'signalled' when the thread exits. Signalled means that any of the wait functions will return.

HTH,
Wes

Sonork ID 100.14017 wtheronjones
QuestionWhere do I get started? Pin
Sherry10-Aug-02 5:58
Sherry10-Aug-02 5:58 
AnswerRe: Where do I get started? Pin
Paul M Watt10-Aug-02 6:26
mentorPaul M Watt10-Aug-02 6:26 
GeneralA _CommandPtr easy question Pin
nss10-Aug-02 5:03
nss10-Aug-02 5:03 
QuestionPostMessage or SendMessage from thread? Pin
Gilfrog10-Aug-02 5:02
Gilfrog10-Aug-02 5:02 
AnswerRe: PostMessage or SendMessage from thread? Pin
Ravi Bhavnani10-Aug-02 5:02
professionalRavi Bhavnani10-Aug-02 5:02 
GeneralRe: PostMessage or SendMessage from thread? Pin
Gilfrog10-Aug-02 5:12
Gilfrog10-Aug-02 5:12 
GeneralRe: PostMessage or SendMessage from thread? Pin
Michael Dunn10-Aug-02 5:16
sitebuilderMichael Dunn10-Aug-02 5:16 
GeneralRe: PostMessage or SendMessage from thread? Pin
Gilfrog10-Aug-02 5:24
Gilfrog10-Aug-02 5:24 
GeneralRe: PostMessage or SendMessage from thread? Pin
Michael Dunn10-Aug-02 6:02
sitebuilderMichael Dunn10-Aug-02 6:02 
AnswerRe: PostMessage or SendMessage from thread? Pin
valikac10-Aug-02 9:17
valikac10-Aug-02 9:17 
GeneralWinMain hidden powers Pin
Georg Haan10-Aug-02 4:24
Georg Haan10-Aug-02 4:24 
GeneralRe: WinMain hidden powers Pin
Michael Dunn10-Aug-02 5:18
sitebuilderMichael Dunn10-Aug-02 5:18 
GeneralCallback Pin
CaesarCZ10-Aug-02 3:59
CaesarCZ10-Aug-02 3:59 
GeneralSimple Text encryption Pin
James Spibey10-Aug-02 3:01
James Spibey10-Aug-02 3:01 
GeneralRe: Simple Text encryption Pin
Chris Maunder10-Aug-02 3:16
cofounderChris Maunder10-Aug-02 3:16 
GeneralRe: Simple Text encryption Pin
James Spibey10-Aug-02 6:26
James Spibey10-Aug-02 6:26 
Generalrunning a game in a window Pin
Kuniva10-Aug-02 1:41
Kuniva10-Aug-02 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.