public static void On(System.Diagnostics.Process process, TimeSpan forHowLong) { var timeoutInMilliseconds = Convert.ToInt32(forHowLong.TotalMilliseconds); process.WaitForExit(timeoutInMilliseconds); if (false == process.HasExited) { process.Kill(); process.WaitForExit(timeoutInMilliseconds); throw new TimeoutException( String.Format( "The process did not exit within the allowed period <{0}> " + "and it has been forcibly closed.", forHowLong ) ); } }
/// <summary> /// Helper function to properly dispose of a process object and ensure it is dead. The given /// reference is set to null afterwards. /// </summary> /// <param name="process">Process to kill. Reference is set to null afterwards.</param> public static void EnsureProcessKill(ref System.Diagnostics.Process process) { if (process == null) { return; } try { process.Kill(); } catch (System.InvalidOperationException) { // This happens if the process has already exited. } process.Dispose(); process = null; }
/// <summary> /// Запуск теста /// </summary> /// <param name="proc">Выполняемый процесс</param> /// <param name="time">Время ожидания выполнения процесса</param> /// <param name="N">Порядковый номер теста</param> public static void beginTest(ref System.Diagnostics.Process proc, int time, int N) { if (proc.WaitForExit(time)) Console.WriteLine("Тест " + N.ToString() + " пройден"); else { Console.WriteLine("Тест " + N.ToString() + " не пройден"); proc.Kill(); } }