Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. wait for CWinThread to terminate -- from UI thread

wait for CWinThread to terminate -- from UI thread

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdesign
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Weyzen
    wrote on last edited by
    #1

    How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

    P P J M 4 Replies Last reply
    0
    • P Peter Weyzen

      How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

      P Offline
      P Offline
      Peter Weyzen
      wrote on last edited by
      #2

      or what about: if I set m_bAutoDelete to FALSE and: PostThreadMessage( WM_QUIT, 0, 0 ); while ( PumpMessage() ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; } delete this;

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

      1 Reply Last reply
      0
      • P Peter Weyzen

        How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?

        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

        P Offline
        P Offline
        pierre_ribery
        wrote on last edited by
        #3

        An example: In your class which creates the thread: private: HANDLE m_StopThread; HANDLE m_WaitThread; static UINT ThreadFunction(LPVOID pvParam); When you create your thread: // Create events m_StopThread = CreateEvent(0, TRUE, FALSE, 0); m_WaitThread = CreateEvent(0, TRUE, FALSE, 0); // Start thread AfxBeginThread(ThreadFunction, this); When you want to terminate the thread: // Trigger thread to stop ::SetEvent(m_StopThread); // Wait until thread finished ::WaitForSingleObject(m_WaitThread, INFINITE); // Close handles ::CloseHandle(m_StopThread); ::CloseHandle(m_WaitThread); //Your thread procedure UINT CFoo::ThreadFunction(LPVOID* pvParam) { CFoo *pParent = static_cast(pvParam); while(true) { // Check event for stop thread if(::WaitForSingleObject(pParent->m_StopThread, 0) == WAIT_OBJECT_0) { // Set event ::SetEvent(pParent->m_WaitThread); return 0; } // Do your processing } } Hope this helps you out!

        1 Reply Last reply
        0
        • P Peter Weyzen

          How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?

          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          That will only work if the thread has a message pump - i.e. is created as a UI thread. If it is a worker thread, I usually do the following (error checking removed for the sake of brevity): in the main UI thread:

          // create worker thread
          m_hTermEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
          m_pThread = AfxBeginThread (ThreadFunction, &m_hTermEvent);

          // time to terminate worker thread
          SetEvent (m_hTermEvent);
          WaitForSingleObject (m_pThread->m_hThread, INFINITE);

          in the worker thread:

          UINT __cdecl ThreadFunction( LPVOID pParam )
          {
          HANDLE hEvent;

          hEvent = \*((HANDLE \*) pParam);
          while (true)
          {
              if WaitForSingleObject (hEvent, 0) == WAIT\_OBJECT\_0)
                  break;
          
              // do your work
          };
          return 0;
          

          }

          Judy

          P 1 Reply Last reply
          0
          • J JudyL_MD

            That will only work if the thread has a message pump - i.e. is created as a UI thread. If it is a worker thread, I usually do the following (error checking removed for the sake of brevity): in the main UI thread:

            // create worker thread
            m_hTermEvent = CreateEvent (NULL, TRUE, FALSE, NULL);
            m_pThread = AfxBeginThread (ThreadFunction, &m_hTermEvent);

            // time to terminate worker thread
            SetEvent (m_hTermEvent);
            WaitForSingleObject (m_pThread->m_hThread, INFINITE);

            in the worker thread:

            UINT __cdecl ThreadFunction( LPVOID pParam )
            {
            HANDLE hEvent;

            hEvent = \*((HANDLE \*) pParam);
            while (true)
            {
                if WaitForSingleObject (hEvent, 0) == WAIT\_OBJECT\_0)
                    break;
            
                // do your work
            };
            return 0;
            

            }

            Judy

            P Offline
            P Offline
            pierre_ribery
            wrote on last edited by
            #5

            That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!

            J P 2 Replies Last reply
            0
            • P pierre_ribery

              That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!

              J Offline
              J Offline
              JudyL_MD
              wrote on last edited by
              #6

              pierre_ribery wrote:

              That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!

              Calm down dude. No need to get snippy. I wasn't responding to your post, I was responding to the original poster. The presence of my post does not imply that yours is wrong. If you'll check the time stamp, we were posting at the same time. Your post didn't exist when I started mine. Judy

              1 Reply Last reply
              0
              • P pierre_ribery

                That is NOT the case. This works as it should for worker threads! Please, show me what in my code would not work!

                P Offline
                P Offline
                Peter Weyzen
                wrote on last edited by
                #7

                Maybe I should go back to basic threading. CWinThread is a UI thread in this case -- and has a message pump. It seems that if I wait for it to terminate, then I am blocking my UI thread. So, it seems I need to pump messages while I wait. Or, do I have no clue? More likely, I am just making this harder than it needs to be. But, the goal is to wait for this thing to terminate completely.

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                1 Reply Last reply
                0
                • P Peter Weyzen

                  How do I terminate my CWinThread from my main UI thread -- without blocking anything. I was thinking I'd try something like: PostThreadMessage( WM_QUIT, 0, 0 ); MSG msg; while( PeekMessage(&msg, NULL, 0,0, PM_REMOVE ) ) { if ( WaitForSingleObject(m_hThread,0) == WAIT_OBJECT_0 ) break; TranslateMessage(&msg); DispatchMessage(&msg); } But that doesn't seem to work... Any suggestions?

                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #8

                  Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:

                  // Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)

                  OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);

                  while (1)
                  {
                      DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);

                  if (dwWaitRet != WAIT_OBJECT_0 + 1)
                          break;

                  MSG msg;
                      while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
                      {
                          AfxGetApp()-> PumpMessage();
                      }
                  }

                  mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  P 2 Replies Last reply
                  0
                  • M Mark Salsbery

                    Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:

                    // Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)

                    OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);

                    while (1)
                    {
                        DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);

                    if (dwWaitRet != WAIT_OBJECT_0 + 1)
                            break;

                    MSG msg;
                        while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
                        {
                            AfxGetApp()-> PumpMessage();
                        }
                    }

                    mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    P Offline
                    P Offline
                    Peter Weyzen
                    wrote on last edited by
                    #9

                    Thank you! -- I knew someone would understand the question!

                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                    1 Reply Last reply
                    0
                    • M Mark Salsbery

                      Are you terminating a UI thread from within itself as you're showing in your code or do you want to terminate a thread from another thread and wait? For the latter, maybe something like this:

                      // Called on main thread to terminate OtherCWinThread (a CWinThread object, UI thread)

                      OtherCWinThread.PostThreadMessage(WM_QUIT, 0, 0);

                      while (1)
                      {
                          DWORD dwWaitRet = ::MsgWaitForMultipleObjects(1, &OtherCWinThread.m_hThread, FALSE, INFINITE, QS_ALLEVENTS);

                      if (dwWaitRet != WAIT_OBJECT_0 + 1)
                              break;

                      MSG msg;
                          while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
                          {
                              AfxGetApp()-> PumpMessage();
                          }
                      }

                      mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      P Offline
                      P Offline
                      Peter Weyzen
                      wrote on last edited by
                      #10

                      ah! insight! I never quite made the distinction. Thanks!

                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups