private ProcessResult WaitForExit() { if (Exited) { throw new InvalidOperationException("Must not execute the process twice"); } if (!Started) { Start(); } while (true) { if (!m_process.HasExited) { try { m_procPeakPagedMemorySize = m_process.PeakPagedMemorySize64; m_procPeakVirtualMemorySize = m_process.PeakVirtualMemorySize64; m_procPeakWorkingSet = m_process.PeakWorkingSet64; if (OnInputRequested != null) { foreach (ProcessThread thread in m_process.Threads) { if (thread.ThreadState == ThreadState.Wait && thread.WaitReason == ThreadWaitReason.UserRequest) { ProcessInputHandleResult result = OnInputRequested(m_outputSinceLastInput.ToString(), m_process.StandardInput); if (result == ProcessInputHandleResult.Handled) { m_outputSinceLastInput.Remove(0, m_outputSinceLastInput.Length); } break; } } } m_process.Refresh(); } catch (InvalidOperationException) { break; } } else { break; } } // Allow any outstanding events to finish m_process.WaitForExit(); Exited = true; return(ProduceResult()); }
private ProcessResult WaitForExit() { if (Exited) { throw new InvalidOperationException("Must not execute the process twice"); } if (!Started) { Start(); } while (true) { if (!m_process.HasExited) { try { m_procPeakPagedMemorySize = m_process.PeakPagedMemorySize64; m_procPeakVirtualMemorySize = m_process.PeakVirtualMemorySize64; m_procPeakWorkingSet = m_process.PeakWorkingSet64; if (OnInputRequested != null) { if (m_child == null) { try { using (var mos = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + m_process.Id)) { using (ManagementObjectCollection collection = mos.Get()) { foreach (ManagementObject mo in collection) { if (m_child != null) { throw new InvalidOperationException("Unexpected number of child processes"); } m_child = Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])); mo.Dispose(); } } } } catch (ArgumentException) { // Process not running // This is okay because it means there is no input to capture and it was a short program } } if (m_child != null) { foreach (ProcessThread thread in m_child.Threads) { if (thread.ThreadState == ThreadState.Wait && thread.WaitReason == ThreadWaitReason.UserRequest) { ProcessInputHandleResult result = OnInputRequested(m_outputSinceLastInput.ToString(), m_process.StandardInput); if (result == ProcessInputHandleResult.Handled) { m_outputSinceLastInput.Remove(0, m_outputSinceLastInput.Length); } break; } } } } m_process.Refresh(); } catch (InvalidOperationException) { break; } } else { break; } } // Allow any outstanding events to finish m_process.WaitForExit(); Exited = true; return(ProduceResult()); }