A process handler will be signaled if it exits.
So, the following will be done (error handling removed for brevity):
BOOL IsProcessRunning(DWORD pid) { HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid); DWORD ret = WaitForSingleObject(process, 0); CloseHandle(process); return ret == WAIT_TIMEOUT; }
Please note that the process identifier can be reworked - it is better to cache the handle that is returned from the CreateProcess call.
You can also use the threadpool API (SetThreadpoolWait in Vista +, RegisterWaitForSingleObject on older platforms) to receive a callback when the process terminates.
EDIT: I missed part of the original βI want to do somethingβ question. You can use this technique if it is normal to have potentially outdated data for a small window or if you want to perform an operation without even trying to complete it. You still have to handle the case when an action fails because the process has completed.
Michael Oct 19 '09 at 21:57 2009-10-19 21:57
source share