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 wait = new ProcessWaitHandle(_processHandle); _registeredWait = ThreadPool.RegisterWaitForSingleObject(wait, (o, t) => { _registeredWait.Unregister(wait); SetExitState(); Exited?.Invoke(this, EventArgs.Empty); _processHandle.Dispose(); wait.Dispose(); }, null, -1, true); _disposable .Add(() => _registeredWait.Unregister(wait)) .Add(_processHandle) .Add(wait); }
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); }