Ejemplo 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);
        }
Ejemplo n.º 2
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);
 }