Wednesday 31 March 2010

Waiting for Thread / Process termination

When launching child processes from applications I used to use GetExitCodeProcess on the process handle, checking if STILL_ACTIVE is returned if it is still running.. heres the smart way!

After spawning a process or creating a worker thread the end of the process/thread can be waited on using WaitForSingleObject.

For instance for threads:

HANDLE hThread;
hThread = CreateThread(NULL,0,StartThread,&threadParam,0,NULL);
if(hThread1 != NULL)
{
WaitForSingleObject(hThread,INFINITE);
}


For processes:

PROCESS_INFORMATION pi={0};
if (CreateProcess (parameters, NULL, NULL, NULL, 0, 0, NULL, NULL, NULL, &pi))
{
WaitForSingleObject(pi.hProcess,INFINITE);
}


1 comment:

Anonymous said...

What's Up everybody, prime chat board I find It incredibly helpful & it has helped me out so much
I hope to be able to contribute and help others like this forum has helped me

_________________
[URL=http://www.certforums.co.uk/forums/lastpostinthread345012.html][B]mcitp enterprise[/B][/URL]