Thread: Messaging Between Threads, Exiting Thread On Demand, Etc.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    41

    Messaging Between Threads, Exiting Thread On Demand, Etc.

    Good afternoon everyone.... Just had a few questions regarding threads if someone wouldn't mind answering.

    I have a main thread (GUI Thread), which creates a secondary thread for processing. There are many times that I want the main thread to signal to the secondary thread to stop and exit immediately. Currently I'm using a global boolean to do this and in the secondary thread periodically checking and then calling ExitThread ( ). I'm really not a big fan of this and depending what part in the secondary thread that is being executed there is not a prompt exit.

    I basically want to set up a message scheme between the two threads so that the main can "PostThreadMessage(WM_EXIT_NOW)" and the secondary thread will check its message queue and then call ExitThread() from there. This would be nice as I could also communicate other things. Is this possible? Below is what I've tried, but the secondary thread never appears to receive a message.

    Code:
    This is global.
    
    HHOOK hook;
    Code:
    This is the main thread.
    
    secondaryThread = CreateThread(NULL, 0, ThreadFunction, NULL, 0, &threadID);
    
    ......
    
    PostThreadMessage(threadID, WM_USER + 5, 1, 1);
    Code:
    Secondary thread
    
    WINAPI threadFunction(...)
    {
      MSG msg;
      PeekMessage(&msg, NULL, WM_USER, WM_USER + 40, PM_NOREMOVE);
      hook = SetWindowsHookEx(WH_GETMESSAGE, &GetMsgProc, NULL, threadID);
    }
    
    LRESULT CALLBACK GetMsgProc(int code, WPARAM wParam, LPARAM lParam)
    {
      ExitThread(1);
      
      UnhookWindowsHookEx(hook);  
      return 1;
    }
    I don't know... I tried to set up a message queue in the secondary thread. Set a hook to catch all incoming messages, and then attempt to process that message, but I never get into the HOOK function.

    Any ideas? Any better approach to this problem?

    P.S. The PeekMessage() line I got from MSDN
    Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue.
    PSS: Also, I am positive the secondary thread has not finished executing before the PostThreadMessage() has been sent.
    Last edited by HyperShadow; 06-08-2007 at 12:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  2. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  3. Thread confusion
    By pyrolink in forum C Programming
    Replies: 0
    Last Post: 01-29-2006, 09:42 PM
  4. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM