[process][windows] Use WaitForSingleObject with a 0 delay in PidExistsWithContext

Reference https://stackoverflow.com/a/6493793

Fixes #1298
pull/1301/head
Lomanic 3 years ago
parent 19771aaf91
commit 7501387fc1

@ -285,8 +285,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
} }
return false, err return false, err
} }
const STILL_ACTIVE = 259 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getexitcodeprocess h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
h, err := windows.OpenProcess(processQueryInformation, false, uint32(pid))
if err == windows.ERROR_ACCESS_DENIED { if err == windows.ERROR_ACCESS_DENIED {
return true, nil return true, nil
} }
@ -296,10 +295,9 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
defer syscall.CloseHandle(syscall.Handle(h)) defer windows.CloseHandle(h)
var exitCode uint32 event, err := windows.WaitForSingleObject(h, 0)
err = windows.GetExitCodeProcess(h, &exitCode) return event == uint32(windows.WAIT_TIMEOUT), err
return exitCode == STILL_ACTIVE, err
} }
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {

Loading…
Cancel
Save