Carbon
Carbon

Reputation: 3943

How to tell if a process started with CreateProcess is still running?

If I've got a process created through CreateProcess(), how would I determine if it's still running? I know I need to use pi.hProcess but I don't know how, and google isn't really giving me meaningful hints.

PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
bool got_it=CreateProcess(NULL, CA2T(launchString.c_str()), NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);

Upvotes: 4

Views: 2131

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596111

You can use any of the standard wait functions, like WaitForSingleObject(), eg:

switch (WaitForSingleObject(pi.hProcess, 0))
{
    case WAIT_OBJECT_0:
        // process has terminated...
        break;

    case WAIT_TIMEOUT:
        // process is still running...
        break;
}

Upvotes: 10

Eelke
Eelke

Reputation: 21993

You can retrieve the process's exit code with GetExitCodeProcess(), which will give the special STILL_ACTIVE value if the process is still running:

DWORD exit_code;
GetExitCodeProcess(pi.hProcess, &exit_code);
if (exit_code == STILL_ACTIVE) {

}

Upvotes: 2

Related Questions