P
Paul G. Tobey [eMVP]
You need to make sure that the thread exits before you try to exit the main
application thread, yes. If a thread is created with CreateThread() and you
keep the handle returned from that call, you have to close the handle to
keep from leaking it. The end of the application is an obvious time to do
that, also. In my unmanaged code, I do something roughly like this to fire
an event which notifies the thread to exit, waits for the thread to exit,
and then closes the thread handle.
if ( hThread != NULL )
{
// Notify thread that it's time for it to terminate.
SetEvent( hEvent );
// Wait for the thread to exit.
WaitForSingleObject( hThread, 5000 ); // Will wait five seconds or
until thread exits.
CloseHandle( hThread );
hThread = NULL;
}
if ( hEvent != NULL )
{
CloseHandle( hEvent ); // Don't leak the event handle, either.
hEvent = NULL;
}
So, you want to end up doing the equivalent steps in managed code...
Paul T.
application thread, yes. If a thread is created with CreateThread() and you
keep the handle returned from that call, you have to close the handle to
keep from leaking it. The end of the application is an obvious time to do
that, also. In my unmanaged code, I do something roughly like this to fire
an event which notifies the thread to exit, waits for the thread to exit,
and then closes the thread handle.
if ( hThread != NULL )
{
// Notify thread that it's time for it to terminate.
SetEvent( hEvent );
// Wait for the thread to exit.
WaitForSingleObject( hThread, 5000 ); // Will wait five seconds or
until thread exits.
CloseHandle( hThread );
hThread = NULL;
}
if ( hEvent != NULL )
{
CloseHandle( hEvent ); // Don't leak the event handle, either.
hEvent = NULL;
}
So, you want to end up doing the equivalent steps in managed code...
Paul T.