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. Termination of a thread

Termination of a thread

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 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.
  • V Offline
    V Offline
    Vassili
    wrote on last edited by
    #1

    Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili

    D M 2 Replies Last reply
    0
    • V Vassili

      Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili

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

      If it is a worker thread, you simply need to exit the thread function. If it is a UI thread, you need to post a WM_QUIT message.

      V 1 Reply Last reply
      0
      • V Vassili

        Hi, I use a thread, whitch I have created with 'AfxBeginThread'. Within this thread I use an endless while loop [while(1) {//do some thing}]. Is it possible to terminate this thread by the thread-caller? All I could find is to use the TerminateThread function but to do this I have to use before the DuplicateHandle function. Unfortunately I was realy not very successfull by trying this. Can anybody help please? Thanks a lot Vassili

        M Offline
        M Offline
        Mike Upton
        wrote on last edited by
        #3

        The sensible way to terminate a thread is to make it terminate itself - generally by setting some sort of flag to signal that the thread should terminate. In its simplest form, the code looks something like this:

        //assumes your bTerminateFlag variable is defined elsewhere - you'd
        //probably actually do this with some sort of class, and pass a pointer
        //to the class instance to your thread function, but I'm keeping this simple
        extern bool bTerminateFlag;

        int YourThreadFunction(LPVOID pParam)
        {
        while (!bTerminateFlag)
        {
        do_something...
        }
        return exit_code;
        }

        And your function to terminate the thread would look something like this:

        int TerminateWorkerThread(HANDLE hWorker)
        {
        bTerminateFlag = true;
        if (WaitForSingleObject(hWorker, INFINITE)!=WAIT_OBJECT_0)
        {
        //an error has occurred
        //handle it somehow
        }
        else
        {
        return GetExitCodeThread(hWorker);
        }
        }

        This is all very simplified - you'd want proper error checking, you probably shouldn't use a global variable as the terminate flag, and you shouldn't really use WaitForSingleObject in a thread with a message loop. But the principle is there. There's generally very little reason to use TerminateThread, and it can lead to all sorts of problems when your thread is doing something worthwhile, and holding resources open, because the terminated thread doesn't get to do any cleanup.


        "We are the knights who say Ni" (The Knights Who Say Ni)

        1 Reply Last reply
        0
        • D David Crow

          If it is a worker thread, you simply need to exit the thread function. If it is a UI thread, you need to post a WM_QUIT message.

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

          Yes, it is a worker thread. In my endless while loop I read the serial port. On an port event I send a message to the main program. I also send a message to the main program with the handle of the thread by using GetCurrentThread() UINT TheThread(LPVOID pParam) { HANDLE* pObject = (HANDLE*)pParam; HANDLE hThread; . . . hThread = GetCurrentThread(); SendMessage(pObject, WM_MY_MESSAGE, 0, (LPARAM) hThread); while(1) { //here I read the CommPort . . } return 0; } --In the main Program: LRESULT CMyDlg::MyFunction(WPARAM wParam, LPARAM lParam) { m_hThread = (HANDLE) lParam; //m_hThread is a private HANDLE variable of CMyDlg return 0L; } void CMyDlg::OnWhatEver() { //TerminateThread(m_hThread, -1); //CloseHandle(m_hThread); } If I use TerminateThread((m_hThread, -1) the application disappears but a process is still running which I have to terminate with the Task Manager. If I use CloseHandle(m_hThread) nothing happens... You posted befor that I need simply to exit the thread function - this is exactly what I want to do, but how?

          D 1 Reply Last reply
          0
          • V Vassili

            Yes, it is a worker thread. In my endless while loop I read the serial port. On an port event I send a message to the main program. I also send a message to the main program with the handle of the thread by using GetCurrentThread() UINT TheThread(LPVOID pParam) { HANDLE* pObject = (HANDLE*)pParam; HANDLE hThread; . . . hThread = GetCurrentThread(); SendMessage(pObject, WM_MY_MESSAGE, 0, (LPARAM) hThread); while(1) { //here I read the CommPort . . } return 0; } --In the main Program: LRESULT CMyDlg::MyFunction(WPARAM wParam, LPARAM lParam) { m_hThread = (HANDLE) lParam; //m_hThread is a private HANDLE variable of CMyDlg return 0L; } void CMyDlg::OnWhatEver() { //TerminateThread(m_hThread, -1); //CloseHandle(m_hThread); } If I use TerminateThread((m_hThread, -1) the application disappears but a process is still running which I have to terminate with the Task Manager. If I use CloseHandle(m_hThread) nothing happens... You posted befor that I need simply to exit the thread function - this is exactly what I want to do, but how?

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

            Vassili wrote: ...I need simply to exit the thread function - this is exactly what I want to do, but how? Change your while(1) loop to have a more meaningful condition. For example:

            bool bContinue = true;

            while (true == bContinue)
            {
            // read port
            // ...
            // if situation warrants, set bContinue = false
            }

            return 0; // this line will cause the thread to exit normally

            V 1 Reply Last reply
            0
            • D David Crow

              Vassili wrote: ...I need simply to exit the thread function - this is exactly what I want to do, but how? Change your while(1) loop to have a more meaningful condition. For example:

              bool bContinue = true;

              while (true == bContinue)
              {
              // read port
              // ...
              // if situation warrants, set bContinue = false
              }

              return 0; // this line will cause the thread to exit normally

              V Offline
              V Offline
              Vassili
              wrote on last edited by
              #6

              Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(

              D R 2 Replies Last reply
              0
              • V Vassili

                Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(

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

                Is the handle being used by ReadFile() synchronous or asynchronous? By your description, it sounds as though a synchronous handle is being used, thus the blocking in which you describe.

                1 Reply Last reply
                0
                • V Vassili

                  Hi again, within the while loop I use the function ReadFile(...). This means that if there is nothing to read from the serial port the process will not step to the next line. It will remain in the ReadFile-step until a new information will come through the port. Because of this I can't use a condition like bContinue. On the other hand only the main program knows when to end the thread. So the question is: is there a way to terminate my thread from the main program? If not then I will have to redesign my complete program :-(

                  R Offline
                  R Offline
                  Rage
                  wrote on last edited by
                  #8

                  [edit] Sorry, did not see you already mentioned it. Why do you use DuplicateHandle?[/edit]

                  BOOL TerminateThread(
                  HANDLE hThread, // handle to the thread
                  DWORD dwExitCode // exit code for the thread
                  );

                  but this is never recommanded... ~RaGE();

                  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