Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 public void WaitForExit(int milliseconds)
 {
     using (ProcessWaitHandle processWaitHandle = new ProcessWaitHandle(_processHandle)) {
         if (processWaitHandle.WaitOne(milliseconds))
         {
             // This means the process exited while waiting.
             SetExitState();
         }
     }
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 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);
 }