Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting if a process is still running

I need to check if a process with a given HANDLE is still running, I tried to do it using the following code however it always returns at the second return false, even if the process is running.

bool isProcessRunning(HANDLE process)
{
    if(process == INVALID_HANDLE_VALUE)return false;

    DWORD exitCode;
    if(GetExitCodeProcess(process, &exitCode) != 0)
        return false;//always returns here

    return GetLastError() == STILL_ACTIVE;//still running
}
like image 741
Fire Lancer Avatar asked Aug 06 '09 11:08

Fire Lancer


People also ask

How do you check if the process is running?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time. This will display the process for the current shell with four columns: PID returns the unique process ID.

How do you check if a PID is still running?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


1 Answers

You can test the process life by using

bool isProcessRunning(HANDLE process)
{
   return WaitForSingleObject( process, 0 ) == WAIT_TIMEOUT;
}
like image 69
decasteljau Avatar answered Sep 18 '22 02:09

decasteljau