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. terminating thread in button on-event

terminating thread in button on-event

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++
8 Posts 5 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.
  • J Offline
    J Offline
    jason99
    wrote on last edited by
    #1

    Hi Everyone, I implemente a thread in a MFC application that can be started and stopped through 2 buttons. My problem: After i click the Stop button, the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. How can i terminate a thread like this using wait for single object? I noticed that some calls to PeekMessage inside the while loop make WaitForSingleObject return WAIT_OBJECT_0. I really don't understand why this isn't working! Please can anybody help?? Jason //------------------------------------------------------------------------- void CAnyDlg::OnStart() { // declarations DWORD dwId; // start the thread m_bThread=true; if(m_hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartThread,(LPVOID)this,0,&dwId)) } //------------------------------------------------------------------------- void CAnyDlg::OnStop() { MSG msg; while(::WaitForSingleObject(m_hThread,100)!=WAIT_OBJECT_0)) { m_bThread=false; } } //------------------------------------------------------------------------- DWORD CAnyDlg::StartThread(LPVOID lpParam) { ((CAnyDlg*)lpParam)->Thread(); return 0; } //------------------------------------------------------------------------- void CAnyDlg::Thread() { while(m_bThread) { m_cstaAnyStatic.SetWindowText("Test"); } } //-------------------------------------------------------------------------

    D V M 3 Replies Last reply
    0
    • J jason99

      Hi Everyone, I implemente a thread in a MFC application that can be started and stopped through 2 buttons. My problem: After i click the Stop button, the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. How can i terminate a thread like this using wait for single object? I noticed that some calls to PeekMessage inside the while loop make WaitForSingleObject return WAIT_OBJECT_0. I really don't understand why this isn't working! Please can anybody help?? Jason //------------------------------------------------------------------------- void CAnyDlg::OnStart() { // declarations DWORD dwId; // start the thread m_bThread=true; if(m_hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartThread,(LPVOID)this,0,&dwId)) } //------------------------------------------------------------------------- void CAnyDlg::OnStop() { MSG msg; while(::WaitForSingleObject(m_hThread,100)!=WAIT_OBJECT_0)) { m_bThread=false; } } //------------------------------------------------------------------------- DWORD CAnyDlg::StartThread(LPVOID lpParam) { ((CAnyDlg*)lpParam)->Thread(); return 0; } //------------------------------------------------------------------------- void CAnyDlg::Thread() { while(m_bThread) { m_cstaAnyStatic.SetWindowText("Test"); } } //-------------------------------------------------------------------------

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      jason99 wrote: ...the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. Nor should it. When the Stop button is clicked, you are waiting 1/10 of a second for the secondary thread object to become signaled. Since there is no code in place to signal it, WAIT_TIMEOUT is returned. Once that happens, m_bThread is set to false, which should cause the loop in CAnyDlg::Thread() to stop, but does nothing for the loop in CAnyDlg::OnStop(), which only stops once WAIT_OBJECT_0 is returned.


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      J 1 Reply Last reply
      0
      • D David Crow

        jason99 wrote: ...the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. Nor should it. When the Stop button is clicked, you are waiting 1/10 of a second for the secondary thread object to become signaled. Since there is no code in place to signal it, WAIT_TIMEOUT is returned. Once that happens, m_bThread is set to false, which should cause the loop in CAnyDlg::Thread() to stop, but does nothing for the loop in CAnyDlg::OnStop(), which only stops once WAIT_OBJECT_0 is returned.


        Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

        J Offline
        J Offline
        jason99
        wrote on last edited by
        #3

        OK, that is right. The first call to WaitForSingle object will surely return WAIT_TIMEOUT. But when the bool is switched, the loop in CAnyDlg::Thread() stops. This means that the created thread will terminate sometime later. From this point on, a continous call to WaitForSingleObject should return WAIT_OBJECT_0 after some time. But that doesn't happen. Tha fact that concerns me is that the code works if i take away the SetWindowText() in the thread or i i call PeekMessage in the WaitForSingleObject loop...this must have something to do with the message queue...

        D 1 Reply Last reply
        0
        • J jason99

          Hi Everyone, I implemente a thread in a MFC application that can be started and stopped through 2 buttons. My problem: After i click the Stop button, the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. How can i terminate a thread like this using wait for single object? I noticed that some calls to PeekMessage inside the while loop make WaitForSingleObject return WAIT_OBJECT_0. I really don't understand why this isn't working! Please can anybody help?? Jason //------------------------------------------------------------------------- void CAnyDlg::OnStart() { // declarations DWORD dwId; // start the thread m_bThread=true; if(m_hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartThread,(LPVOID)this,0,&dwId)) } //------------------------------------------------------------------------- void CAnyDlg::OnStop() { MSG msg; while(::WaitForSingleObject(m_hThread,100)!=WAIT_OBJECT_0)) { m_bThread=false; } } //------------------------------------------------------------------------- DWORD CAnyDlg::StartThread(LPVOID lpParam) { ((CAnyDlg*)lpParam)->Thread(); return 0; } //------------------------------------------------------------------------- void CAnyDlg::Thread() { while(m_bThread) { m_cstaAnyStatic.SetWindowText("Test"); } } //-------------------------------------------------------------------------

          V Offline
          V Offline
          valikac
          wrote on last edited by
          #4

          One solution is a boolean variable to indicate the status of the loop. Kuphryn

          1 Reply Last reply
          0
          • J jason99

            OK, that is right. The first call to WaitForSingle object will surely return WAIT_TIMEOUT. But when the bool is switched, the loop in CAnyDlg::Thread() stops. This means that the created thread will terminate sometime later. From this point on, a continous call to WaitForSingleObject should return WAIT_OBJECT_0 after some time. But that doesn't happen. Tha fact that concerns me is that the code works if i take away the SetWindowText() in the thread or i i call PeekMessage in the WaitForSingleObject loop...this must have something to do with the message queue...

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            If this secondary thread's handle is closed while the wait is still pending, WaitForSingleObject()'s behavior is undefined. Have you seen these two articles: http://www.flounder.com/workerthreads.htm http://www.flounder.com/uithreads.htm

            1 Reply Last reply
            0
            • J jason99

              Hi Everyone, I implemente a thread in a MFC application that can be started and stopped through 2 buttons. My problem: After i click the Stop button, the while-loop doesn't break, meaning that WaitForSingleObject does never return WAIT_OBJECT_0. How can i terminate a thread like this using wait for single object? I noticed that some calls to PeekMessage inside the while loop make WaitForSingleObject return WAIT_OBJECT_0. I really don't understand why this isn't working! Please can anybody help?? Jason //------------------------------------------------------------------------- void CAnyDlg::OnStart() { // declarations DWORD dwId; // start the thread m_bThread=true; if(m_hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartThread,(LPVOID)this,0,&dwId)) } //------------------------------------------------------------------------- void CAnyDlg::OnStop() { MSG msg; while(::WaitForSingleObject(m_hThread,100)!=WAIT_OBJECT_0)) { m_bThread=false; } } //------------------------------------------------------------------------- DWORD CAnyDlg::StartThread(LPVOID lpParam) { ((CAnyDlg*)lpParam)->Thread(); return 0; } //------------------------------------------------------------------------- void CAnyDlg::Thread() { while(m_bThread) { m_cstaAnyStatic.SetWindowText("Test"); } } //-------------------------------------------------------------------------

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              Aha! m_cstaAnyStatic.SetWindowText causes a WM_SETTEXT message to be sent to the message queue. However, while in the loop in CAnyDlg::Stop, the UI thread can't process the message (this assumes that the static control and the dialog box were created by the same thread and hence share that thread's message queue). You're effectively deadlocked. It's typically a bad idea to directly modify any controls on the UI thread from a worker thread. It's better to define a custom message and post (not send) messages to the window to effect any changes in UI state. PostMessage just queues up the message, then returns; SendMessage blocks until the message is received by the window procedure, processed, and a response generated (either by returning from the window procedure, or by calling ReplyMessage).

              D 1 Reply Last reply
              0
              • M Mike Dimmick

                Aha! m_cstaAnyStatic.SetWindowText causes a WM_SETTEXT message to be sent to the message queue. However, while in the loop in CAnyDlg::Stop, the UI thread can't process the message (this assumes that the static control and the dialog box were created by the same thread and hence share that thread's message queue). You're effectively deadlocked. It's typically a bad idea to directly modify any controls on the UI thread from a worker thread. It's better to define a custom message and post (not send) messages to the window to effect any changes in UI state. PostMessage just queues up the message, then returns; SendMessage blocks until the message is received by the window procedure, processed, and a response generated (either by returning from the window procedure, or by calling ReplyMessage).

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Good catch!! The post vs. send message problem is common, but is sometimes hidden in the problem description.


                Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                A 1 Reply Last reply
                0
                • D David Crow

                  Good catch!! The post vs. send message problem is common, but is sometimes hidden in the problem description.


                  Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  Hi guys, thanx for the hints! I will try the PostMessage()!

                  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