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. CWinThread - How to ask is still running?

CWinThread - How to ask is still running?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 3 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.
  • X Offline
    X Offline
    x87Bliss
    wrote on last edited by
    #1

    My progress dialog creates a worker thread using AfxBeginThread. When the user clicks cancel, I don't want the window to close if the thread is still running. I want to have the thread safely finish first. There is a BOOL variable that the worker thread checks on occasion while running. If this is true, it begins to cancel. The worker thread only reads this variable, the Progress Dialog writes to it if needed. Should the worker thread be suspended before this variable is written? Or will it be OK since they will never Write at the same time. Secondly, and most importantly, I currently have the Worker Thread Post a WM_USER message, with certain WPARAM and LPARAM for the reason, when it has ended. Is there any other way to check if the thread is running or not, and what the controlling function's return value was? Thanks

    J S 2 Replies Last reply
    0
    • X x87Bliss

      My progress dialog creates a worker thread using AfxBeginThread. When the user clicks cancel, I don't want the window to close if the thread is still running. I want to have the thread safely finish first. There is a BOOL variable that the worker thread checks on occasion while running. If this is true, it begins to cancel. The worker thread only reads this variable, the Progress Dialog writes to it if needed. Should the worker thread be suspended before this variable is written? Or will it be OK since they will never Write at the same time. Secondly, and most importantly, I currently have the Worker Thread Post a WM_USER message, with certain WPARAM and LPARAM for the reason, when it has ended. Is there any other way to check if the thread is running or not, and what the controlling function's return value was? Thanks

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      There are two methods. I've included code with both. I used a bool to signal the other thread, but vastly prefer using events since threads often have waits within them. Also note that I can't remember the last time I checked a thread exit code, but you may have a reason. Finally, if you don't call MFC within the thread (with a few exceptions), you could just use _beginthreadex(). I just did this last week. 1) Call AfxBeingThread() with the CREATE_SUSPENDED flag. 2) set CWinThread::m_bAutoDelete to false 3) Call CWinThread::Resume() 4) At exit, after setting the bool (or setting an event, which is my preferred method), call WaitOnSingleObject() with CWinThread::m_hThread 5) Call GetThreadExitCode() to get exit code 6) Delete the CWinThread object I've run into problems with MFC complaining about item 6.

      CWinThread* StartAfxThread(AFX_THREADPROC pThreadProc, LPVOID pParam = NULL, int priority = THREAD_PRIORITY_NORMAL)
      {
      CWinThread* pThread = ::AfxBeginThread(pThreadProc, pParam, priority, 0, CREATE_SUSPENDED);
      pThread->m_bAutoDelete = false;
      pThread->ResumeThread();
      return pThread;
      }

      CWinThread\* pThread = StartAfxThread(TheThread);
      
      isRunning = false;
      
      if (::WaitForSingleObject(pThread->m\_hThread, 1000) == WAIT\_TIMEOUT)
      {
      	::TerminateThread(pThread->m\_hThread, (DWORD) -1);
      	Sleep(10);
      }
      
      DWORD exitCode = 0;
      ::GetExitCodeThread(pThread->m\_hThread, &exitCode);
      \_tprintf(\_T("Exit code: %u"), exitCode);
      
      delete pThread;
      
      1. Call AfxBeingThread() with the CREATE_SUSPENDED flag. 2) Duplicate CWindThread::m_hThread 3) Call CWinThread::Resume() 4) At exit, after setting the bool, call WaitOnSingleObject() with the duplicate handle. 5) Call GetThreadExitCode() to get exit code 6) Close the duplicate handle

      HANDLE StartAfxThread(AFX_THREADPROC pThreadProc, LPVOID pParam = NULL, int priority = THREAD_PRIORITY_NORMAL)
      {
      CWinThread* pThread = ::AfxBeginThread(pThreadProc, pParam, priority, 0, CREATE_SUSPENDED);

      HANDLE hProcess = GetCurrentProcess();
      HANDLE hDup;
      if (!::DuplicateHandle(hProcess, pThread->m\_hThread, hProcess, &hDup, 0, FALSE, DUPLICATE\_SAME\_ACCESS))
      	hDup = NULL;
      
      pThread->ResumeThread();
      return hDup;
      

      }

      HANDLE hThread = StartAfxThread(TheThread);
      
      isRunning = false; // signal thread to stop
      
      if (::WaitForSingleObject(hThread, 1000) == WAIT\_TIMEOUT)
      {
      	::TerminateThread(hThread, (DWORD) -1);
      	Sleep(10);
      }
      
      DWORD exitCode = 0;
      ::GetExitCodeThread
      
      1 Reply Last reply
      0
      • X x87Bliss

        My progress dialog creates a worker thread using AfxBeginThread. When the user clicks cancel, I don't want the window to close if the thread is still running. I want to have the thread safely finish first. There is a BOOL variable that the worker thread checks on occasion while running. If this is true, it begins to cancel. The worker thread only reads this variable, the Progress Dialog writes to it if needed. Should the worker thread be suspended before this variable is written? Or will it be OK since they will never Write at the same time. Secondly, and most importantly, I currently have the Worker Thread Post a WM_USER message, with certain WPARAM and LPARAM for the reason, when it has ended. Is there any other way to check if the thread is running or not, and what the controlling function's return value was? Thanks

        S Offline
        S Offline
        sudhir_Kumar
        wrote on last edited by
        #3

        When the user clicks cancel, I don't want the window to close if the thread is still running. I want to have the thread safely finish first. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< DWORD exitcode; GetExitCodeThread(hTread->m_hThread, &exitcode); if (exitcode != STILL_ACTIVE ) OnCancel();

        -@SuDhIrKuMaR@-

        J 1 Reply Last reply
        0
        • S sudhir_Kumar

          When the user clicks cancel, I don't want the window to close if the thread is still running. I want to have the thread safely finish first. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< DWORD exitcode; GetExitCodeThread(hTread->m_hThread, &exitcode); if (exitcode != STILL_ACTIVE ) OnCancel();

          -@SuDhIrKuMaR@-

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          By default a CWinThread object with auto delete itself upon thread exit. Not only will the thread handle within be invalid, the pointer itself will be invalid.

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          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