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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Terminate a CWinThread

How to Terminate a CWinThread

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
18 Posts 7 Posters 25 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.
  • C Cedric Moonen

    A better solution would be to use a loop that finishes when a certain variable is false: // In your Thread function: while (bContinue) { // Do Things } Thus, when this variable becomes false, the function exists and your thread terminates. And to be really clean, this variable should be a member variable of your dialog class. Then, when you need to close the Thread you do: bContinue = false; // Wait for your thread to terminate with the WaitForSingleObject function Hope this helps

    A Offline
    A Offline
    a_du
    wrote on last edited by
    #6

    but for CWinThread, the thread process funtion is a static fucntion. the parameters are passed into thread fucntion by the "LPVOID param" argument. How can i pass a class member attribute into the thread function? Thanx.

    C 1 Reply Last reply
    0
    • N Nilesh K

      Post a user defined message with PostThreadMessage and exit the thread on receiving this message.

      - Nilesh "Reading made Don Quixote a gentleman. Believing what he read made him mad" -George Bernard Shaw

      A Offline
      A Offline
      a_du
      wrote on last edited by
      #7

      Can you give me a sample of this mechanism. by calling PostThreadMessage( ) how can i implement the CWinThread so that it can catch the message? i have ever tried this way: threadRoute() { GetMessage(.....) } but is not what i want, because i need a loop inside the thread fucntion. it is keep looping until i want to terminate. Thanx!

      N 1 Reply Last reply
      0
      • A a_du

        but for CWinThread, the thread process funtion is a static fucntion. the parameters are passed into thread fucntion by the "LPVOID param" argument. How can i pass a class member attribute into the thread function? Thanx.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #8

        I suppose you create your thread within a class function ? If yes then pass the this pointer to the function and inside that function, call a public member function of your class. That's it.

        A 2 Replies Last reply
        0
        • A a_du

          I use AfxBeginThread to create a thread in which, there is loop keep reading a log file and display the contents. This thread is a memeber of a dialog. when i exit the dialog by click "Cancel" button. how can i terminate the thread when i click the "cancel" button? I see some one said can use PostThreadMessage, but how can i catch the message as the thread process is a loop fuction. Pls Help.

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #9

          Hello, I think that you have a worker thread problem. Read here[^] about threads. As stated in the article, you can kill your thread by calling ::TerminateThead((HANDLE)_YourWinThreadObject_);, but this is not safe, since your resources will not be cleaned up nicely... Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          • A a_du

            Actually i am not sur :( i create a dlg which has a control list. when this dlg is created by calling domodal(), a CWinThread is created in OnInit funtion. The thread will keep reading the log files and display the contents on the control list. User will click cancel of the dlg to exit the dlg. problem is if i don't kill the Thread, it may still hold the handle to the log file. so further writing to the log file will cause problem.

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #10

            could you show me your coding of OnInitDialog Function?

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta

            1 Reply Last reply
            0
            • C Cedric Moonen

              I suppose you create your thread within a class function ? If yes then pass the this pointer to the function and inside that function, call a public member function of your class. That's it.

              A Offline
              A Offline
              a_du
              wrote on last edited by
              #11

              en. Thanx. i try it now.

              1 Reply Last reply
              0
              • A a_du

                Can you give me a sample of this mechanism. by calling PostThreadMessage( ) how can i implement the CWinThread so that it can catch the message? i have ever tried this way: threadRoute() { GetMessage(.....) } but is not what i want, because i need a loop inside the thread fucntion. it is keep looping until i want to terminate. Thanx!

                N Offline
                N Offline
                Nilesh K
                wrote on last edited by
                #12

                Simpler way out could be checking a flag in your thread's loop and this flag be changed by your application before exiting.

                - Nilesh "Reading made Don Quixote a gentleman. Believing what he read made him mad" -George Bernard Shaw

                1 Reply Last reply
                0
                • C Cedric Moonen

                  I suppose you create your thread within a class function ? If yes then pass the this pointer to the function and inside that function, call a public member function of your class. That's it.

                  A Offline
                  A Offline
                  a_du
                  wrote on last edited by
                  #13

                  Using Pointer really can change access the data have been passed into the thread. thanx a lot. One more question is that is there any potential problem if uing this method. i mean , if the thread loop is reading the status while outside fuction change the status at same time.

                  C 1 Reply Last reply
                  0
                  • A a_du

                    Using Pointer really can change access the data have been passed into the thread. thanx a lot. One more question is that is there any potential problem if uing this method. i mean , if the thread loop is reading the status while outside fuction change the status at same time.

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #14

                    Yes, there can be problems so it is better to use a CCriticalSection for both reading and writing to the variable. Take a look at the MSDN doc, it is not really complicated to use. If you have any further questions, don't hesitate to ask.

                    A 1 Reply Last reply
                    0
                    • A a_du

                      Actually i am not sur :( i create a dlg which has a control list. when this dlg is created by calling domodal(), a CWinThread is created in OnInit funtion. The thread will keep reading the log files and display the contents on the control list. User will click cancel of the dlg to exit the dlg. problem is if i don't kill the Thread, it may still hold the handle to the log file. so further writing to the log file will cause problem.

                      P Offline
                      P Offline
                      pierrekande
                      wrote on last edited by
                      #15

                      There is several way to terminate your thread. Let me suppose that you understand about synchronisation. And also to make thing more easier, define a static member variable of type: static BOOL m_endLoop for your dialog class CYourDLG; and also define a static member of type: static CEvent m_EventExit for your dialog class CYourDLG. in the CYourDLG::OnInit() function initialize m_endLoop=0; and call m_EventExit.SetEvent(). And in your function OnCancel and OnOk at before to exit, call WaitForSingleObject(EventExit,INFINITE); and in the first line in those 2 functions set m_endLoop =TRUE; and now in your loop function that terminate the thread, inside the loop insert this code: if(CYourDLG::m_endLoop==TRUE) { // first to some cleaning and close all opened file or whatelse ........... //and call CYourDLG::m_EventExit.SetEvent(); return 0; } ================== BUT when your start the thread insert this line at the begin of the thread function CYourDLG::m_EventExit.ResetEvent(); That is. You will have no access violation. This solution must help. Let me know if you have more questions Perre Kande

                      1 Reply Last reply
                      0
                      • A a_du

                        I use AfxBeginThread to create a thread in which, there is loop keep reading a log file and display the contents. This thread is a memeber of a dialog. when i exit the dialog by click "Cancel" button. how can i terminate the thread when i click the "cancel" button? I see some one said can use PostThreadMessage, but how can i catch the message as the thread process is a loop fuction. Pls Help.

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

                        See here for termination methods.


                        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                        1 Reply Last reply
                        0
                        • A a_du

                          Actually i am not sur :( i create a dlg which has a control list. when this dlg is created by calling domodal(), a CWinThread is created in OnInit funtion. The thread will keep reading the log files and display the contents on the control list. User will click cancel of the dlg to exit the dlg. problem is if i don't kill the Thread, it may still hold the handle to the log file. so further writing to the log file will cause problem.

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

                          a_du wrote: Actually i am not sur How can you not know? You call AfxBeginThread() in one of two ways. One way creates a worker (has no message pump) thread. The other creates a UI (has a message pump) thread.


                          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            Yes, there can be problems so it is better to use a CCriticalSection for both reading and writing to the variable. Take a look at the MSDN doc, it is not really complicated to use. If you have any further questions, don't hesitate to ask.

                            A Offline
                            A Offline
                            a_du
                            wrote on last edited by
                            #18

                            :-DThank you very much. I have fixed my problem. I have a question about the GetExitCodeThread function. if AfxEndThread fucntion is called inside my thread, what value i can get use GetExitCodeThread function. dose AfxEndThread function really can deallocate all stack of hold by the thread. Thank you.

                            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