private Win32Process(NativeMethods.PROCESS_INFORMATION pi, StreamWriter stdin, StreamReader stdout, StreamReader stderror) { StandardInput = stdin; StandardOutput = stdout; StandardError = stderror; _hasExited = false; _exitCodeLock = new object(); Id = pi.dwProcessId; MainThreadId = pi.dwThreadId; _processHandle = new SafeProcessHandle(pi.hProcess, true); var threadHandle = new SafeThreadHandle(pi.hThread); var wait = new ProcessWaitHandle(_processHandle); _registeredWait = ThreadPool.RegisterWaitForSingleObject(wait, (o, t) => { _registeredWait.Unregister(wait); SetExitState(); Exited?.Invoke(this, EventArgs.Empty); _processHandle.Close(); threadHandle.Close(); wait.Close(); }, null, -1, true); _disposable .Add(() => _registeredWait.Unregister(wait)) .Add(_processHandle) .Add(threadHandle) .Add(wait); }
public void WaitForExit(int milliseconds) { using (ProcessWaitHandle processWaitHandle = new ProcessWaitHandle(_processHandle)) { if (processWaitHandle.WaitOne(milliseconds)) { // This means the process exited while waiting. SetExitState(); } } }
public bool WaitForExit(int milliseconds) { using (var processWaitHandle = new ProcessWaitHandle(_processHandle)) { if (processWaitHandle.WaitOne(milliseconds)) { // This means the process exited while waiting. SetExitState(); return(true); } } return(false); }
private Win32Process(NativeMethods.PROCESS_INFORMATION pi) { _hasExited = false; _exitCodeLock = new object(); ProcessId = pi.dwProcessId; MainThreadId = pi.dwThreadId; _processHandle = new SafeProcessHandle(pi.hProcess, true); _threadHandle = new SafeThreadHandle(pi.hThread); _wait = new ProcessWaitHandle(_processHandle); _registeredWait = ThreadPool.RegisterWaitForSingleObject(_wait, (o, t) => { _registeredWait.Unregister(_wait); SetExitState(); Exited?.Invoke(this, new Win32ProcessExitEventArgs(_exitCode)); _processHandle.Close(); _threadHandle.Close(); _wait.Close(); }, null, -1, true); }