protected void OnExecuteCompleted(ConsoleProcessAsynchronousHandle objHandle, ConsoleProcessResult objResult) { if (OutputReceived != null) { ExecuteCompleted(objHandle, objResult); } }
protected void OnOutputReceived(ConsoleProcessAsynchronousHandle objHandle, string strOutputLine) { if (OutputReceived != null) { OutputReceived(objHandle, strOutputLine); } }
private void ExecuteAsynchronousProc(object objArguments) { ConsoleProcessResult objResult = null; ConsoleProcessAsynchronousHandle objHandle = (ConsoleProcessAsynchronousHandle)objArguments; List <string> objOutputLines = new List <string>(); ProcessStartInfo objStartInfo = CreateProcessStartInfo(objHandle.FilePath, objHandle.Arguments); using (Process objProcess = new Process()) { /// We define the delegate like this since there is no way for us /// to group multiple running asynchronous processes using the traditional /// method. Defining the delegate this way will allow us to utilize the /// objects tha have already been defined within this thread. /// objProcess.OutputDataReceived += delegate(object objSender, DataReceivedEventArgs objData) { if (String.IsNullOrEmpty(objData.Data) == false) { objOutputLines.Add(objData.Data); OnOutputReceived(objHandle, objData.Data); } }; objProcess.StartInfo = objStartInfo; objProcess.EnableRaisingEvents = true; objProcess.Start(); objProcess.BeginOutputReadLine(); objResult = null; while (objProcess.HasExited == false) { if (objHandle.Cancelled == true) { objProcess.Kill(); objResult = new ConsoleProcessResult(objHandle.FilePath, objHandle.Arguments, ConsoleProcessResultType.Cancelled, objHandle.Interpreter); break; } TimeSpan objDuration = DateTime.Now - objProcess.StartTime; if (objDuration.TotalSeconds > objHandle.TimeOut) { objProcess.Kill(); objResult = new ConsoleProcessResult(objHandle.FilePath, objHandle.Arguments, ConsoleProcessResultType.TimedOut, objHandle.Interpreter); break; } objProcess.WaitForExit(10); } if (objResult == null) { objResult = new ConsoleProcessResult(objHandle.FilePath, objHandle.Arguments, ConsoleProcessResultType.Completed, objProcess.ExitCode, objOutputLines, objHandle.Interpreter); } } OnExecuteCompleted(objHandle, objResult); }
public ConsoleProcessAsynchronousHandle ExecuteAsynchronous(string strFilePath, string strArguments, int intTimeOut, ConsoleProcessResultInterpreter objInterpreter) { ParameterizedThreadStart objParameterizedThreadStart = new ParameterizedThreadStart(ExecuteAsynchronousProc); Thread objExecuteAsynchronousThread = new Thread(objParameterizedThreadStart); objExecuteAsynchronousThread.IsBackground = true; ConsoleProcessAsynchronousHandle objHandle = new ConsoleProcessAsynchronousHandle(strFilePath, strArguments, intTimeOut, objInterpreter); objExecuteAsynchronousThread.Start(objHandle); return(objHandle); }