Programming Tips - How can I tell if a process is running?

Date: 2010may13 OS: Windows Language: C/C++ Q. How can I tell if a process is running? A. By attempting to get the exit code, like this:
BOOL IsProcessRunning(const HANDLE hProcess) { DWORD dwExitCode; if (!GetExitCodeProcess(hProcess, &dwExitCode)) return FALSE; return dwExitCode == STILL_ACTIVE; } void ExampleUse() { if (IsProcessRunning(hMyProcess)) { printf("My process IS running\n"); } else { printf("My process is NOT running\n"); } }
If you just want to wait until a process is done it is better to do:
WaitForSingleObject(hProcess, INFINITE);