Win32, MFC: thread termination

I have a DLL that has a CWinThread class called CWork. I create it using AfxBeginThread.

In this class, I defined a procedure that will loop endlessly and perform a specific task. This procedure will be used as a thread in itself. I create it also using AfxBeginThread.

Now that my dll exits, I would like to end the thread. This is because I have a crash on exit and I am afraid that this is the reason.

In addition, there is

Pseudo-code example:

class cmain

Cmain::Cmain(){
    pMyThread = AfxBeginThread(CWork - a CWinThread based Class);
}

 UINT HandleNextVTSConnectionCommandProc(LPVOID pParam);

class CWork

 CWork:CWork(){
   AfxBeginThread(HandleNextVTSConnectionCommandProc, this);
 }


UINT HandleNextVTSConnectionCommandProc(LPVOID pParam){

 while(true){

     dosomething();
     sleep(2000);

 }

}

My question is: what is the correct way to terminate these two threads?

Thank!

+4
source share
4 answers

, - , , . Windows , , HANDLE. , , .

+5

, CreateEvent, . , (SetEvent) (WaitForSingleObject .)

HandleNextVTSConnectionCommandProc while(true)

while(WaitForSingleObject(hEvent, 0) != WAIT_OBJECT_0)

. , .

+4

Set the flag instead of using while (true) to tell your thread when it should end. You can also use the event.

You should also wait for your thread to complete before exiting, so you should use (in the main code, as soon as you signal the end of the thread):

WaitForSingleObject(thread_handle)
+2
source

Something like this should start:

HANDLE g_hThreadExitRequest = NULL;

UINT __cdecl ThreadFunction(LPVOID pParam)
{
    AllocConsole();
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);

    for (int i=1; true; ++i)
    {
        CStringA count;
        count.Format("%d\n", i);
        WriteFile(hCon, (LPCSTR)count, count.GetLength(), NULL, NULL);

        if (WaitForSingleObject(g_hThreadExitRequest, 1000) == WAIT_OBJECT_0)
            break;
    }

    // We can do any thread specific cleanup here.
    FreeConsole();

    return 0;
}

void Go()
{
    // Create the event we use the request the thread exit.
    g_hThreadExitRequest = CreateEvent(
                                NULL,   // LPSECURITY_ATTRIBUTES lpEventAttributes
                                TRUE,   // BOOL bManualReset
                                FALSE,  // BOOL bInitialState
                                NULL    // LPCTSTR lpName
                                );

    // We create the thread suspended so we can mess with the returned CWinThread without
    // MFC auto deleting it when the thread finishes.
    CWinThread *pThread = AfxBeginThread(
                                &ThreadFunction,        // AFX_THREADPROC pfnThreadProc
                                NULL,                   // LPVOID pParam
                                THREAD_PRIORITY_NORMAL, // int nPriority
                                0,                      // UINT nStackSize
                                CREATE_SUSPENDED ,      // DWORD dwCreateFlags
                                NULL                    // LPSECURITY_ATTRIBUTES lpSecurityAttrs
                                );

    // Turn off MFC auto delete "feature".
    pThread->m_bAutoDelete = FALSE;

    // Start the thread running.
    pThread->ResumeThread();

    // Wait 30 seconds.
    Sleep(30*1000);

    // Signal the thread to exit and wait for it to do so.
    SetEvent(g_hThreadExitRequest);
    WaitForSingleObject(pThread->m_hThread, INFINITE);

    // Delete the CWinTread object since we turned off auto delete.
    delete pThread;

    // We're finished with the event.
    CloseHandle(g_hThreadExitRequest);
    g_hThreadExitRequest = NULL;
}
+1
source

All Articles