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. Stopping a thread

Stopping a thread

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
15 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.
  • S Sujan Christo

    Hi suhredayan, Thanx for your reply. It's a worker thread. TerminateThread is working but i am using critical section for data handling, so MSDN states that it is a DANGEROUS function. Think that i must have to use waitforsingleobject(). Sujan

    V Offline
    V Offline
    Vikash Dubey 0
    wrote on last edited by
    #4

    The thread must finish execution on its own based on some condition. Use WaitForSingleObject or WaitForMultipleObjects if you got wait for some signal and u can also give time. There are other methods also that can be used to stop execution like signalling a flag etc.

    S 1 Reply Last reply
    0
    • S Sujan Christo

      Hi suhredayan, Thanx for your reply. It's a worker thread. TerminateThread is working but i am using critical section for data handling, so MSDN states that it is a DANGEROUS function. Think that i must have to use waitforsingleobject(). Sujan

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #5

      avoid calling TerminateThread [^]
      There is no spoon. mail

      S 1 Reply Last reply
      0
      • 2 224917

        avoid calling TerminateThread [^]
        There is no spoon. mail

        S Offline
        S Offline
        Sujan Christo
        wrote on last edited by
        #6

        Hi Thanx for the link i'll look into it Sujan

        1 Reply Last reply
        0
        • V Vikash Dubey 0

          The thread must finish execution on its own based on some condition. Use WaitForSingleObject or WaitForMultipleObjects if you got wait for some signal and u can also give time. There are other methods also that can be used to stop execution like signalling a flag etc.

          S Offline
          S Offline
          Sujan Christo
          wrote on last edited by
          #7

          Hi Dubey, Thanx 4 ur reply.I'll try to implement it using waitformultipleobjects. Sujan

          1 Reply Last reply
          0
          • S Sujan Christo

            Hi All, I used SuspendThread and ExitInstance to stop a thread created using AfxBeginThread Function. But in some newsgroup messages it is stated that this is not the right method. I've used PostThreadMessage using WM_QUIT, but this is not working. So How to stop a thread created using AfxBeginThread function. Thanx in Advance Sujan

            A Offline
            A Offline
            Andrzej Markowski
            wrote on last edited by
            #8

            You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:

            //In your thread class:
            //1. Declare m_eventShutdown variable of the type CEvent.
            //2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
            //
            //In your thread function:
            //
            int CYourThreadClass::ThreadFunction()
            {
            while(1)
            {
            HANDLE h[2];
            h[0] = m_eventShutdown.m_hObject;
            h[1] = another handle;
            DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
            if(dwRet==WAIT_OBJECT_0)
            return 0; //exit thread
            else if(dwRet==WAIT_OBJECT_1)
            //handle your synchronization object
            else if(dwRet==WAIT_TIMEOUT)
            //handle timeout
            else if(dwRet==WAIT_FAILED)
            // handle error
            }
            }
            //
            //To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.

            S 2 Replies Last reply
            0
            • A Andrzej Markowski

              You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:

              //In your thread class:
              //1. Declare m_eventShutdown variable of the type CEvent.
              //2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
              //
              //In your thread function:
              //
              int CYourThreadClass::ThreadFunction()
              {
              while(1)
              {
              HANDLE h[2];
              h[0] = m_eventShutdown.m_hObject;
              h[1] = another handle;
              DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
              if(dwRet==WAIT_OBJECT_0)
              return 0; //exit thread
              else if(dwRet==WAIT_OBJECT_1)
              //handle your synchronization object
              else if(dwRet==WAIT_TIMEOUT)
              //handle timeout
              else if(dwRet==WAIT_FAILED)
              // handle error
              }
              }
              //
              //To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.

              S Offline
              S Offline
              Sujan Christo
              wrote on last edited by
              #9

              Hi Markowski Thanx a lot. I am working with it. Sujan

              1 Reply Last reply
              0
              • S Sujan Christo

                Hi All, I used SuspendThread and ExitInstance to stop a thread created using AfxBeginThread Function. But in some newsgroup messages it is stated that this is not the right method. I've used PostThreadMessage using WM_QUIT, but this is not working. So How to stop a thread created using AfxBeginThread function. Thanx in Advance Sujan

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

                Does this article help?


                "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                S 1 Reply Last reply
                0
                • D David Crow

                  Does this article help?


                  "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                  S Offline
                  S Offline
                  Sujan Christo
                  wrote on last edited by
                  #11

                  Yes Yes Thanx David. Now i am working with it. Sujan

                  1 Reply Last reply
                  0
                  • A Andrzej Markowski

                    You could create a shutdown event to stop the worker thread execution. Here's an example how to do it:

                    //In your thread class:
                    //1. Declare m_eventShutdown variable of the type CEvent.
                    //2. Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
                    //
                    //In your thread function:
                    //
                    int CYourThreadClass::ThreadFunction()
                    {
                    while(1)
                    {
                    HANDLE h[2];
                    h[0] = m_eventShutdown.m_hObject;
                    h[1] = another handle;
                    DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
                    if(dwRet==WAIT_OBJECT_0)
                    return 0; //exit thread
                    else if(dwRet==WAIT_OBJECT_1)
                    //handle your synchronization object
                    else if(dwRet==WAIT_TIMEOUT)
                    //handle timeout
                    else if(dwRet==WAIT_FAILED)
                    // handle error
                    }
                    }
                    //
                    //To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.

                    S Offline
                    S Offline
                    Sujan Christo
                    wrote on last edited by
                    #12

                    Hi Andrzej One silly question. Sorry. How to manage with WAIT_OBJECT_1.Instead shall i use the created thread handle. I used it and it is always getting into that "Wait Timeout" state. OR have to create another event. Thanx in advance Sujan

                    A 1 Reply Last reply
                    0
                    • S Sujan Christo

                      Hi Andrzej One silly question. Sorry. How to manage with WAIT_OBJECT_1.Instead shall i use the created thread handle. I used it and it is always getting into that "Wait Timeout" state. OR have to create another event. Thanx in advance Sujan

                      A Offline
                      A Offline
                      Andrzej Markowski
                      wrote on last edited by
                      #13

                      Here's an example which manages with WAIT_OBJECT_1. The example demonstates how to print to the debug output every 1 s:

                      // In your thread class:
                      // Declare m_eventShutdown variable of the type CEvent.
                      // Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
                      // Declare m_eventPrintToDebug variable of the type CEvent.
                      // Add function CYourThreadClass::PrintToDebug { m_eventPrintToDebug.SetEvent();}
                      //
                      //In your thread function:
                      //
                      int CYourThreadClass::ThreadFunction()
                      {
                      while(1)
                      {
                      int nCounter = 0;
                      HANDLE h[2];
                      h[0] = m_eventShutdown.m_hObject;
                      h[1] = m_eventPrintToDebug.m_hObject;
                      DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE)
                      if(dwRet==WAIT_OBJECT_0) // exit thread
                      return 0;
                      else if(dwRet==WAIT_OBJECT_1) // print to the debug output
                      TRACE("Counter = %d\n",n_Counter++);
                      else if(dwRet==WAIT_TIMEOUT)
                      // never here if the time-out interval = INFINITE
                      else if(dwRet==WAIT_FAILED)
                      // handle error
                      }
                      }
                      //
                      //In your UI thread call SetTimer(1,1000,NULL) and in OnTimer call CYourThreadClass::PrintToDebug.
                      //To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.

                      S 1 Reply Last reply
                      0
                      • A Andrzej Markowski

                        Here's an example which manages with WAIT_OBJECT_1. The example demonstates how to print to the debug output every 1 s:

                        // In your thread class:
                        // Declare m_eventShutdown variable of the type CEvent.
                        // Add function CYourThreadClass::ShutdownThread { m_eventShutdown.SetEvent();}
                        // Declare m_eventPrintToDebug variable of the type CEvent.
                        // Add function CYourThreadClass::PrintToDebug { m_eventPrintToDebug.SetEvent();}
                        //
                        //In your thread function:
                        //
                        int CYourThreadClass::ThreadFunction()
                        {
                        while(1)
                        {
                        int nCounter = 0;
                        HANDLE h[2];
                        h[0] = m_eventShutdown.m_hObject;
                        h[1] = m_eventPrintToDebug.m_hObject;
                        DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE)
                        if(dwRet==WAIT_OBJECT_0) // exit thread
                        return 0;
                        else if(dwRet==WAIT_OBJECT_1) // print to the debug output
                        TRACE("Counter = %d\n",n_Counter++);
                        else if(dwRet==WAIT_TIMEOUT)
                        // never here if the time-out interval = INFINITE
                        else if(dwRet==WAIT_FAILED)
                        // handle error
                        }
                        }
                        //
                        //In your UI thread call SetTimer(1,1000,NULL) and in OnTimer call CYourThreadClass::PrintToDebug.
                        //To terminate the worker thread call CYourThreadClass::ShutdownThread from your UI thread.

                        S Offline
                        S Offline
                        Sujan Christo
                        wrote on last edited by
                        #14

                        Hi Andrzej, Thanx A Lot. I am working with it. Actually i creating a number of threads using AfxBeginThread(..) function and trying to end them. The code goes like this CWinThread *multthreads[MAX_CONNECTIONS]; int count; UINT DataAccessThreading(LPVOID pParam) { CoInitialize(NULL); int* ind= (int *)pParam; int index=*ind; free(ind); while(TRUE) { HANDLE h[2]; h[0] = m_eventShutdown.m_hObject; h[1] = multthreads[count]->m_hThread; DWORD dwRets = (DWORD) multthreads[count]->m_hThread; DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE); if(dwRet==WAIT_OBJECT_0) { TRACE("THREAD EXIT SUCCESSFULL-Not Killing \n"); return 0; //exit thread } else if(dwRet== (DWORD) multthreads[count]->m_hThread)//handle your synchronization object { test.CacheRefresh(index); Sleep(refreshInterval); //Added by Sujan } else if(dwRet==WAIT_TIMEOUT)//handle timeout { TRACE("Wait Timeout \n"); } else if(dwRet==WAIT_FAILED)// handle error { TRACE("Wait Failed \n"); } } return 0; } and the OnInitDialog i am creating a thread by using int *p=(int *)malloc(sizeof(int)); *p=i; multthreads[i]=AfxBeginThread(DataAccessThreading,p,0,0,NULL); Am i going on the right way. Please comment on this. While using this code i am not getting my event called at all. SORRY FOR DISTURBING YOU. AGAIN Thanx A Lot Sujan

                        S 1 Reply Last reply
                        0
                        • S Sujan Christo

                          Hi Andrzej, Thanx A Lot. I am working with it. Actually i creating a number of threads using AfxBeginThread(..) function and trying to end them. The code goes like this CWinThread *multthreads[MAX_CONNECTIONS]; int count; UINT DataAccessThreading(LPVOID pParam) { CoInitialize(NULL); int* ind= (int *)pParam; int index=*ind; free(ind); while(TRUE) { HANDLE h[2]; h[0] = m_eventShutdown.m_hObject; h[1] = multthreads[count]->m_hThread; DWORD dwRets = (DWORD) multthreads[count]->m_hThread; DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE); if(dwRet==WAIT_OBJECT_0) { TRACE("THREAD EXIT SUCCESSFULL-Not Killing \n"); return 0; //exit thread } else if(dwRet== (DWORD) multthreads[count]->m_hThread)//handle your synchronization object { test.CacheRefresh(index); Sleep(refreshInterval); //Added by Sujan } else if(dwRet==WAIT_TIMEOUT)//handle timeout { TRACE("Wait Timeout \n"); } else if(dwRet==WAIT_FAILED)// handle error { TRACE("Wait Failed \n"); } } return 0; } and the OnInitDialog i am creating a thread by using int *p=(int *)malloc(sizeof(int)); *p=i; multthreads[i]=AfxBeginThread(DataAccessThreading,p,0,0,NULL); Am i going on the right way. Please comment on this. While using this code i am not getting my event called at all. SORRY FOR DISTURBING YOU. AGAIN Thanx A Lot Sujan

                          S Offline
                          S Offline
                          Sujan Christo
                          wrote on last edited by
                          #15

                          Hi Andrzej, If you have got the problem with my code. I am waiting for your responce. SORRY 4 DISTRUBING U Much Sujan

                          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