CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Worker Thread

  1. #1
    Join Date
    Apr 2000
    Location
    Romania, Cluj-Napoca
    Posts
    57

    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.

  2. #2
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    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.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  3. #3
    Join Date
    May 2002
    Posts
    1
    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

  4. #4
    Join Date
    Jan 2000
    Posts
    2
    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
  •  





Click Here to Expand Forum to Full Width

Featured