Example #1
0
        public ConsoleProcessResult ExecuteSynchronous(string strFilePath, string strArguments, int intTimeOut, ConsoleProcessResultInterpreter objInterpreter)
        {
            ConsoleProcessResult objResult = null;

            ProcessStartInfo objStartInfo = CreateProcessStartInfo(strFilePath, strArguments);

            try
            {
                using (Process objProcess = Process.Start(objStartInfo))
                {
                    bool blnResult = objProcess.WaitForExit(intTimeOut * 1000);
                    if (blnResult == true)
                    {
                        string        strOutputLine;
                        List <string> objOutputLines = new List <string>();
                        while ((strOutputLine = objProcess.StandardOutput.ReadLine()) != null)
                        {
                            objOutputLines.Add(strOutputLine);
                        }

                        objResult = new ConsoleProcessResult(strFilePath, strArguments, ConsoleProcessResultType.Completed, objProcess.ExitCode, objOutputLines, objInterpreter);
                    }
                    else
                    {
                        objResult = new ConsoleProcessResult(strFilePath, strArguments, ConsoleProcessResultType.TimedOut, -1, objInterpreter);
                    }
                }
            }
            catch (Exception objException)
            {
                objResult = new ConsoleProcessResult(strFilePath, strArguments, objException, objInterpreter);
            }

            return(objResult);
        }
Example #2
0
        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);
        }
Example #3
0
        public static InterpretedConsoleProcessResult DefaultInterpreter(ConsoleProcessResult objConsoleProcessResult)
        {
            bool   blnSuccess = (objConsoleProcessResult.ProcessResult == ConsoleProcessResultType.Completed);
            string strMessage = ((objConsoleProcessResult.ProcessException != null) ? objConsoleProcessResult.ProcessException.Message : string.Empty);

            InterpretedConsoleProcessResult objInterpretedResult = new InterpretedConsoleProcessResult(blnSuccess, strMessage);

            return(objInterpretedResult);
        }
Example #4
0
 protected void OnExecuteCompleted(ConsoleProcessAsynchronousHandle objHandle, ConsoleProcessResult objResult)
 {
     if (OutputReceived != null)
     {
         ExecuteCompleted(objHandle, objResult);
     }
 }