none
how to judge whether a certain thread has been created and is running? RRS feed

  • Question

  • AfxBeginThread(BMPtoJPEGThread,(void*)this);  // in addtion, how to close the thread?    thank you 

    -------------------------------------------------------------------------------

    static UINT BMPtoJPEGThread(LPVOID pParam)
    {
     tabTwo * tabTwoIntance = (tabTwo*)pParam;

     while ( TRUE )
     {
      if ( WaitForSingleObject(hEvent,INFINITE) == WAIT_OBJECT_0 && tabTwoIntance->isGenerate)
      {
       EnterCriticalSection(&csConvert);
       if ( ! filenames.empty()  )
       {   
        CString temp, original;
           original = temp = filenames.front();
        filenames.pop();    
        temp.Replace(_T("\\temppic"),_T("\\temppicJPG"));
        temp.Replace(_T(".bmp"),_T(".jpeg"));
        BMP2JPG(original,temp);
       }
       else
       {
        ResetEvent(hEvent);
       }
       LeaveCriticalSection(&csConvert);

      }
     }

    }

    Tuesday, November 24, 2009 12:57 PM

Answers

  • You can use GetExitCodeThread, if it returns STILL_ACTIVE, then the thread is still running. Also WaitForSingleObject can be used with a timeout of zero, and if return value is WAIT_TIMEOUT, the thread must be running (you got to test this).

    To kill a thread use PostThreadMessage(...WM_QUIT...). If nothing works, unleash the beast, TerminateThread (I'm not responsible ;)).


    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    • Marked as answer by cutedevil Tuesday, November 24, 2009 2:42 PM
    Tuesday, November 24, 2009 1:10 PM
    Moderator

All replies

  • You can use GetExitCodeThread, if it returns STILL_ACTIVE, then the thread is still running. Also WaitForSingleObject can be used with a timeout of zero, and if return value is WAIT_TIMEOUT, the thread must be running (you got to test this).

    To kill a thread use PostThreadMessage(...WM_QUIT...). If nothing works, unleash the beast, TerminateThread (I'm not responsible ;)).


    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    • Marked as answer by cutedevil Tuesday, November 24, 2009 2:42 PM
    Tuesday, November 24, 2009 1:10 PM
    Moderator
  • Close the thread by returning from the thread function (BMPtoJPEGThread).

    Tuesday, November 24, 2009 2:25 PM