The advantage of using WaitForSingleObject when checking the process ID

What is the advantage of using WaitForSingleObject here rather than using it? The first block of code is the previous answer . The second block is how I do it.

 BOOL IsProcessRunning(DWORD pid) { HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid); DWORD ret = WaitForSingleObject(process, 0); CloseHandle(process); return (ret == WAIT_TIMEOUT); } 

against

 BOOL IsProcessRunning(DWORD pid) { HANDLE process = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); const bool exists = (process != NULL); CloseHandle(process); return exists; } 

It seems that using SYNCHRONIZE requires higher privileges, and I only want to check the PID for the current user.

+6
source share
3 answers

When the process ends, it stops working, but it does not fail until the last handle is closed. The first solution distinguishes between these two states (still in progress or in progress). Of course, the answer may be outdated at the time of its return.

If you don't need this distinction, then your approach is fine (although I would call the function something like DoProcessExist).

+8
source

An IsProcessRunning implementation can also use the Win32 GetExitCodeProcess API.

+1
source

WaitForSingleObject will wait for the process to complete or complete. Only then will he return.

-1
source

Source: https://habr.com/ru/post/927766/


All Articles