Example #1
0
        public static int WaitForProcessFinished(
            Process process, ProcessOutputProcessor stdout, ProcessOutputProcessor stderr)
        {
            int exitCode;

            try
            {
                CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStartInfo,
                                                           process.Id, process.StartInfo.FileName, process.StartInfo.Arguments);
                process.WaitForExit();
                var stopFlag = false;
                var index    = 0;
                while (process.StandardOutput.Peek() >= 0)
                {
                    var line = process.StandardOutput.ReadLine();
                    CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStdOutDump,
                                                               process.Id, line);
                    if (stdout != null && !stopFlag)
                    {
                        stopFlag = stdout(new ProcessOutputProcessorState(line, ++index));
                    }
                }
                stopFlag = false;
                index    = 0;
                while (process.StandardError.Peek() >= 0)
                {
                    var line = process.StandardError.ReadLine();
                    CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStdErrDump,
                                                               process.Id, line);
                    if (stderr != null && !stopFlag)
                    {
                        stopFlag = stderr(new ProcessOutputProcessorState(line, ++index));
                    }
                }
                exitCode = process.ExitCode;
                CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessExitCode,
                                                           process.Id, exitCode);
                process.Close();
            }
            catch (Exception ex)
            {
                CoreApplication.Instance.Logger.LogError(Message.Common_ProcessExecutionFailed,
                                                         process.StartInfo.FileName, process.StartInfo.Arguments, ex.Message);
                exitCode = PROCESS_EXECUTION_FAILED;
            }
            return(exitCode);
        }
Example #2
0
 public static int StartProcessAndWaitForFinished(
     string command, string parameters, ProcessOutputProcessor stdout, ProcessOutputProcessor stderr)
 {
     try
     {
         var startInfo = new ProcessStartInfo(command, parameters ?? String.Empty)
         {
             CreateNoWindow         = true,
             UseShellExecute        = false,
             RedirectStandardOutput = true,
             RedirectStandardError  = true
         };
         using (var process = Process.Start(startInfo))
         {
             return(WaitForProcessFinished(process, stdout, stderr));
         }
     }
     catch (Exception ex)
     {
         CoreApplication.Instance.Logger.LogError(Message.Common_ProcessStartFailed, command, parameters, ex.Message);
         return(PROCESS_START_FAILED);
     }
 }
Example #3
0
 public static int StartProcessAndWaitForFinished( 
     string command, string parameters, ProcessOutputProcessor stdout, ProcessOutputProcessor stderr)
 {
     try
     {
         var startInfo = new ProcessStartInfo(command, parameters ?? String.Empty)
         {
             CreateNoWindow = true,
             UseShellExecute = false,
             RedirectStandardOutput = true,
             RedirectStandardError = true
         };
         using (var process = Process.Start(startInfo))
         {
             return WaitForProcessFinished(process, stdout, stderr);
         }
     }
     catch (Exception ex)
     {
         CoreApplication.Instance.Logger.LogError(Message.Common_ProcessStartFailed, command, parameters, ex.Message);
         return PROCESS_START_FAILED;
     }
 }
Example #4
0
 public static int WaitForProcessFinished( 
     Process process, ProcessOutputProcessor stdout, ProcessOutputProcessor stderr)
 {
     int exitCode;
     try
     {
         CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStartInfo,
             process.Id, process.StartInfo.FileName, process.StartInfo.Arguments);
         process.WaitForExit();
         var stopFlag = false;
         var index = 0;
         while (process.StandardOutput.Peek() >= 0)
         {
             var line = process.StandardOutput.ReadLine();
             CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStdOutDump,
                 process.Id, line);
             if (stdout != null && !stopFlag)
             {
                 stopFlag = stdout(new ProcessOutputProcessorState(line, ++index));
             }
         }
         stopFlag = false;
         index = 0;
         while (process.StandardError.Peek() >= 0)
         {
             var line = process.StandardError.ReadLine();
             CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessStdErrDump,
                 process.Id, line);
             if (stderr != null && !stopFlag)
             {
                 stopFlag = stderr(new ProcessOutputProcessorState(line, ++index));
             }
         }
         exitCode = process.ExitCode;
         CoreApplication.Instance.Logger.LogVerbose(Message.Common_ProcessExitCode,
             process.Id, exitCode);
         process.Close();
     }
     catch (Exception ex)
     {
         CoreApplication.Instance.Logger.LogError(Message.Common_ProcessExecutionFailed,
             process.StartInfo.FileName, process.StartInfo.Arguments, ex.Message);
         exitCode = PROCESS_EXECUTION_FAILED;
     }
     return exitCode;
 }