示例#1
0
 protected void OnExecuteCompleted(ConsoleProcessAsynchronousHandle objHandle, ConsoleProcessResult objResult)
 {
     if (OutputReceived != null)
     {
         ExecuteCompleted(objHandle, objResult);
     }
 }
示例#2
0
 protected void OnOutputReceived(ConsoleProcessAsynchronousHandle objHandle, string strOutputLine)
 {
     if (OutputReceived != null)
     {
         OutputReceived(objHandle, strOutputLine);
     }
 }
示例#3
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);
        }
示例#4
0
        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);
        }