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. How to Kill the Worker Thread ?

How to Kill the Worker Thread ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++iostutorialquestion
23 Posts 6 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.
  • R Rajesh R Subramanian

    Having looked at your code in your first post, I can tell you that you MUST NOT use TerminateThread(). It's saddening to see you've picked the worst suggestion that was given to you.

    It is a crappy thing, but it's life -^ Carlo Pallini

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

    Rajesh R Subramanian wrote:

    It's saddening to see you've picked the worst suggestion that was given to you.

    That's usually the case because it looks like the easiest solution :sigh:

    Cédric Moonen Software developer
    Charting control [v1.5] OpenGL game tutorial in C++

    1 Reply Last reply
    0
    • R Rajesh R Subramanian

      First of all, you must use AfxBeginThread, if you're using MFC[^].

      void CThreadDemoDlg::OnEnd(){
      SetEvent(hEndAllEvent);
      }

      UINT WorkerThreadXYZ(LPVOID pParams){

      fstream OutFile;
      OutFile.open("FileOne.txt",ios::out);

      for(int i=0;i<10000;i++)
      {
      if(WaitForSingleObject(hEndAllEvent, 0) == WAIT_OBJECT_0) break;
      OutFile << i << " ";
      }

      OutFile.close();
      return FALSE; //Return false while using CWinThread, unless you know what you're doing, or unless you like memory leaks.
      }

      Have a look at WaitForSingleObject[^]

      It is a crappy thing, but it's life -^ Carlo Pallini

      S Offline
      S Offline
      Suresh H
      wrote on last edited by
      #11

      Hello Everyone,, Sorry saw all yr response now.Thanks a lot for the reply. I am very much new to the Thread Concept. thought of learning the basics of threads. can anyone suggest me some nice Article to start with. Thanking you, Suresh

      R 1 Reply Last reply
      0
      • S Suresh H

        Hi Parag Patel, Thanks for the reply. I tried with this below code but still the thread is running its not terminated, can u please tell how to kill the thread.

        void CThreadDemoDlg::OnEnd()
        {
        // TODO: Add your control notification handler code here

        TerminateThread(hr1,1);
        TerminateThread(hr2,1);
        

        }

        P Offline
        P Offline
        ParagPatel
        wrote on last edited by
        #12

        Can you paste your code snap here? You can also use flag mechanism, its good solution.

        //globle variable
        HANDLE hr1,hr2;
        bool stopTh; //default set to false.

        void CThreadDemoDlg::OnStart()
        {
        // TODO: Add your control notification handler code here

        stopTh = false;
        
        hr1 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadOne,this,0,0);
        hr2 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadTwo,this,0,0);
        

        }
        void CThreadDemoDlg::OnEnd()
        {
        // TODO: Add your control notification handler code here

        stopTh = true;
        

        }

        UINT WorkerThreadOne(LPVOID Param)
        {
        fstream OutFile;
        OutFile.open("FileOne.txt",ios::out);

        for(int i=0;i<10000;i++)
        {
        	if(stopTh == true)
        		break;
        	OutFile  << i << " ";
        }
        OutFile.close();
        return true;
        

        }

        Why 1.0/5.0?

        Parag Patel

        modified on Thursday, April 16, 2009 9:40 AM

        D 1 Reply Last reply
        0
        • C Cedric Moonen

          Maybe you didn't see the replies to his post but it is strongly recommanded not to use TerminateThread. A better approach would be to pass a pointer to your dialog class to the thread function, and within the thread cast it back to your dialog and call a public function on it. Inside that function you put the code you already provided except that in your loop, you also check if a flag is set or not (this flag is a simple bool which is a member of the dialog). If the flag is set, you simply stop the loop and the function terminates. When you click on the end button, you can simply set this flag so that the loop stops.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          S Offline
          S Offline
          Suresh H
          wrote on last edited by
          #13

          Hello Cédric Moonen,, Sorry saw yr response now.Thanks a lot for the reply. I am very much new to the Thread Concept. thought of learning the basics of threads. can anyone suggest me some nice basic Article to start with. Thanking you, Suresh

          C 1 Reply Last reply
          0
          • S Suresh H

            Hello Cédric Moonen,, Sorry saw yr response now.Thanks a lot for the reply. I am very much new to the Thread Concept. thought of learning the basics of threads. can anyone suggest me some nice basic Article to start with. Thanking you, Suresh

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

            This[^] article is one of the best I know of.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            R 1 Reply Last reply
            0
            • S Suresh H

              Hello Everyone,, Sorry saw all yr response now.Thanks a lot for the reply. I am very much new to the Thread Concept. thought of learning the basics of threads. can anyone suggest me some nice Article to start with. Thanking you, Suresh

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #15

              There are plenty of articles around, but before all that, do you have a book on MFC programming? If not, please buy one. My book recommendations for MFC are here[^] Essay recommendation: Worker Threads[^] UI Threads[^] There are plenty of articles at CP as well, just do a search.

              It is a crappy thing, but it's life -^ Carlo Pallini

              1 Reply Last reply
              0
              • C Cedric Moonen

                This[^] article is one of the best I know of.

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #16

                It looks like we both gave him the same article to read, at the same time (20 mins back, as of now). :)

                It is a crappy thing, but it's life -^ Carlo Pallini

                C 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  It looks like we both gave him the same article to read, at the same time (20 mins back, as of now). :)

                  It is a crappy thing, but it's life -^ Carlo Pallini

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

                  It seems that somebody is voting 1 to all of our messages. Can you guess who it is ? :rolleyes:

                  Cédric Moonen Software developer
                  Charting control [v1.5] OpenGL game tutorial in C++

                  R 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    It seems that somebody is voting 1 to all of our messages. Can you guess who it is ? :rolleyes:

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #18

                    I know, but I'm ignoring that. I've been balancing the votes. Forget it, the votes will be balanced by the regulars. :)

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    1 Reply Last reply
                    0
                    • S Suresh H

                      Hello Everyone, I have Demo MFC application, with 2 buttons Start and End Button. Start button will start 2 threads

                      void CThreadDemoDlg::OnStart()
                      {
                      // TODO: Add your control notification handler code here

                      HANDLE hr1,hr2;
                      hr1 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadOne,this,0,0);
                      hr2 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadTwo,this,0,0);
                      

                      }

                      UINT WorkerThreadOne(LPVOID Param)
                      {
                      fstream OutFile;
                      OutFile.open("FileOne.txt",ios::out);

                      for(int i=0;i<10000;i++)
                      	OutFile  << i << " ";
                      OutFile.close();
                      return true;
                      

                      }

                      Can anyone please tell me how to kill the thread ? OnEnd click. Thanking you, Suresh.

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

                      Suresh H wrote:

                      for(int i=0;i<10000;i++)

                      You need to add another condition to this loop, something like:

                      CEvent m_event; // start as non-signaled

                      for (int i = 0; i < 10000; i++)
                      {
                      // set the event to signaled when the End button is clicked
                      if (WaitForSingleObject(m_event.m_hObject, 0) == WAIT_OBJECT_0)
                      break;
                      }

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      1 Reply Last reply
                      0
                      • P ParagPatel

                        Can you paste your code snap here? You can also use flag mechanism, its good solution.

                        //globle variable
                        HANDLE hr1,hr2;
                        bool stopTh; //default set to false.

                        void CThreadDemoDlg::OnStart()
                        {
                        // TODO: Add your control notification handler code here

                        stopTh = false;
                        
                        hr1 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadOne,this,0,0);
                        hr2 = CreateThread(NULL,0,(unsigned long (\_\_stdcall \*)(void \*))WorkerThreadTwo,this,0,0);
                        

                        }
                        void CThreadDemoDlg::OnEnd()
                        {
                        // TODO: Add your control notification handler code here

                        stopTh = true;
                        

                        }

                        UINT WorkerThreadOne(LPVOID Param)
                        {
                        fstream OutFile;
                        OutFile.open("FileOne.txt",ios::out);

                        for(int i=0;i<10000;i++)
                        {
                        	if(stopTh == true)
                        		break;
                        	OutFile  << i << " ";
                        }
                        OutFile.close();
                        return true;
                        

                        }

                        Why 1.0/5.0?

                        Parag Patel

                        modified on Thursday, April 16, 2009 9:40 AM

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

                        ParagPatel wrote:

                        if(stopTh == true)

                        Unless stopTh is volatile, the compiler will optimize out this check.

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        P 1 Reply Last reply
                        0
                        • D David Crow

                          ParagPatel wrote:

                          if(stopTh == true)

                          Unless stopTh is volatile, the compiler will optimize out this check.

                          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          P Offline
                          P Offline
                          ParagPatel
                          wrote on last edited by
                          #21

                          Thanks , Yes.. stopTH must be globle or thread function must use through passed class object.

                          Parag Patel Sr. Software Eng, Varaha Systems

                          D R 2 Replies Last reply
                          0
                          • P ParagPatel

                            Thanks , Yes.. stopTH must be globle or thread function must use through passed class object.

                            Parag Patel Sr. Software Eng, Varaha Systems

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

                            ParagPatel wrote:

                            Thanks Simmons,

                            :confused:

                            ParagPatel wrote:

                            Yes.. stopTH must be globle

                            Being global has nothing to do with it. If the compiler detects that nothing in the loop is changing that variable, it will optimize out the check. So even if the secondary thread changes that variable, it will go unseen.

                            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            1 Reply Last reply
                            0
                            • P ParagPatel

                              Thanks , Yes.. stopTH must be globle or thread function must use through passed class object.

                              Parag Patel Sr. Software Eng, Varaha Systems

                              R Offline
                              R Offline
                              Rajesh R Subramanian
                              wrote on last edited by
                              #23

                              Changing it through the class object = changing it from a different thread (the main thread of the app). Which means it will be optimized away.

                              It is a crappy thing, but it's life -^ Carlo Pallini

                              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