|
-
May 17th, 2002, 06:29 AM
#1
Worker Thread
I have two threads: a UI thread and a worker thread. From time to time I send some messages from the worker thread to the UI thread in order to update some user interface elements.
That's ok. But I was thinking (as an exercise), what if, I send messages from the UI thread to the worker thread.
Of course the worker thread does not have a message queue, but if I create a hidden window, and in the main loop of the worker thread I will check for incoming messages , it will work ?
What do U think about. Does anyone tried this ????
10x in advance
Good answers will be rated.
-
May 17th, 2002, 06:51 AM
#2
This would work, of course, as long as your worker thread has a running message pump. But you don't need a hidden window. You can simply process the messages.
-
May 17th, 2002, 09:58 AM
#3
A user interface thread has a message pump by definition.
Use the class wizard to derive a class from CWinThread.
This will provide you a message pump for free.
Starting the thread is of the form:
CMyThread* m_pMyThread = (CMyThread*)AfxBeginThread(RUNTIME_CLASS(CMyThread));
Sending messages is through PostThreadMessage
Closing the thread is by message as well:
m_pMyThread->PostThreadMessage(WM_QUIT,0,0);
I am sure there are more detail articles on this floating around.
chris
-
May 17th, 2002, 01:26 PM
#4
I have simulated the sendMessage function with the worker thread with events.
1. Before PostThreadMessage I reset a named event.
2. The calling process/thread will wait for the event to set after calling the PostthreadMessage
3. in the worker thread once the process corresponding the PostThreadMessage is over, set the Named Event.
4. That causes the calling process/thread to come out of the waiting state.
That worked for me, you can try also.
/ockumar
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|