Click to See Complete Forum and Search --> : Terminating A Thread


db_from_mn
December 3rd, 2004, 07:53 AM
I've created a thread in which I have a message loop for receiving PnP notifications.
It's working, but now I need to ensure that the thread terminates when I'm done with it.
All of this code is running in a dll and the window that's receiving the messages is not visible.
How do stop the polling loop when I'm done with it?
Do I have to send it a Quit event from another thread, such as the thread that created it?
What if the thread that created this thread terminates? Will this one automatically terminate?

DWORD WINAPI MessageLoop( LPVOID lpParam )
{

// Local Variables

// Register a window class for receiving PnP notifications using egisterClassEx...

// Create a window for receiving PnP notifications using CreateWindowEx

// Register for Device Notifications using RegisterDeviceNotification

// Implement a message loop

MSG msg;
while( GetMessage( &msg, hNotificationWindow, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}

return 0;
}

bigBA
December 3rd, 2004, 07:59 AM
you can use an event.

create the event before you create the thread and give the thread the event as parameter.
in the loop, the thread should test the event if it is signaled. (and if it is, terminate/return)

in your main programm: when you want the thread to terminate, just set the event to signaled state.


other question:
how do you create the thread?

db_from_mn
December 3rd, 2004, 08:39 AM
Hello bigBA,
That makes sense, except that GetMessage( &msg, hNotificationWindow, 0, 0 ) is blocking, waiting for a message. How can I get it to return if my special termination event is signaled?

I create the thread like this:

hPollingLoopThread = ::CreateThread(
NULL, // default security attributes
0, // use default stack size
MessageLoop, // thread function
&hUCan2ApiInstance, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier

Note that hUCan2ApiInstance is set by AfxGetInstanceHandle(), in the CreateInstance member function of my dll.

bigBA
December 3rd, 2004, 09:22 AM
you have to use the ::PeekMessage API, this won't block until a message arrives. PeekMessage will return FALSE if no messages are availabe.

So... You can do something like this:

struct ThreadParams
{
HINSTANCE hInstance,
HANDLE hTerminateEvent;
};

//function whicht starts and monitors the thread
{
HANDLE hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);

ThreadParams stThreadParams;
stThreadParams.hInstance = AfxGetInstanceHandle();
stThreadParams.hTerminateEvent = hEvent;

//now fire off your thread
HANDLE hThread = ::CreateThread(..., (LPARAM)&stThreadParams, ...);

//do something

//say the thread to terminate
::SetEvent(hEvent);

//wait until thread has terminated
WaitForSingleObject(hThread, INFINITE);

::CloseHandle(hThread);
::CloseHandle(hEvent);
}

//in thread function
{
ThreadParams* pstThreadParams = (ThreadParams*)lParam;

//initialize

//do the loop until event is signaled
while( WaitForSingleObject(pstThreadParams->hTerminateEvent, 0) != WAIT_OBJECT_0 )
{
if( ::PeekMessage(....) )
{
//Translate and Dispatch
}
}

//return from thread function
}

Andreas Masur
December 3rd, 2004, 09:30 AM
[ Moved thread ]

Andreas Masur
December 3rd, 2004, 09:32 AM
Well...basically the answer could have been found in the following FAQ (http://www.codeguru.com/forum/showthread.php?t=305166)...

However, if your thread function already have a message loop, simply send a message to it such as 'WM_QUIT'...

db_from_mn
December 3rd, 2004, 10:11 AM
Thank you all for your help.
I got this to work to my satisfaction by sending a WM_QUIT message to the notification window, as Andreas suggested.
Thanks a lot,
Dennis

bigBA
December 3rd, 2004, 10:11 AM
Well...basically the answer could have been found in the following FAQ (http://www.codeguru.com/forum/showthread.php?t=305166)...

However, if your thread function already have a message loop, simply send a message to it such as 'WM_QUIT'...

basically, yes.

but i think in his architecture, the only thing which will happen, that the window will close... but the thread stays alive and tries to catch messages of the destroyed window.

bigBA
December 3rd, 2004, 10:50 AM
well.. ok :)

should thought of that possibility earlier... :rolleyes: