Exemplo n.º 1
0
        private static void HandleProcessExit(
            Process process,
            ProcessExitInfo result)
        {
            var exited = process.WaitForExit(Consts.ProcessExecutionTimeout);

            if (exited)
            {
                result.ExitCode = process.ExitCode;
                result.IsError  = result.ExitCode != ReturnCode.Successful;
            }
            else
            {
                result.IsError = true;
                process.KillProcessAndChildren();
            }
        }
 /// <summary>
 /// Execute a process and check for a clean exit to determine if the process exists.
 /// </summary>
 /// <param name="programName">Name of the program to start.</param>
 /// <param name="args">Command line argumentes provided to the program.</param>
 /// <param name="waitTime">Time to wait for the process to close.</param>
 /// <param name="criterion">Function to evaluate the process response to determine success. The default implementation returns true if the exit code equals 0.</param>
 /// <returns></returns>
 public static bool Probe(string programName, string args = "", int waitTime = 3000, Func <ProcessExitInfo, bool> criterion = null)
 {
     try
     {
         var process = new Process
         {
             StartInfo = new ProcessStartInfo
             {
                 FileName  = programName,
                 Arguments = args,
                 RedirectStandardOutput = true,
                 RedirectStandardError  = true,
                 UseShellExecute        = false
             }
         };
         var stdout = new List <string>();
         var stderr = new List <string>();
         process.OutputDataReceived += (s, e) => stdout.Add(e.Data);
         process.ErrorDataReceived  += (s, e) => stderr.Add(e.Data);
         process.Start();
         process.BeginErrorReadLine();
         process.BeginOutputReadLine();
         process.WaitForExit(waitTime);
         var exitInfo = new ProcessExitInfo {
             ExitCode = process.ExitCode, StdOut = stdout, StdErr = stderr
         };
         var exitCode = process.ExitCode;
         return(criterion == null ? exitInfo.ExitCode == 0 : criterion(exitInfo));
     }
     catch (InvalidOperationException)
     {
         // The excutable failed to execute prior wait time expiring.
         return(false);
     }
     catch (SystemException)
     {
         // The excutable doesn't exist on path. Rather than handling Win32 exception, chose to handle a less platform specific sys exception.
         return(false);
     }
 }
Exemplo n.º 3
0
        private static void ReadOutput(string outputFileName, ProcessExitInfo result)
        {
            if (File.Exists(outputFileName))
            {
                var count = 5;
                while (count > 0)
                {
                    try
                    {
                        result.Output = File.ReadAllLines(outputFileName);
                        break;
                    }
                    catch (IOException err)
                    {
                        Debug.WriteLine(err.Message);
                        count -= 1;
                        Thread.Sleep(500);
                    }
                }
            }

            File.Delete(outputFileName);
        }
Exemplo n.º 4
0
        private static ProcessExitInfo RunProcess(string appName, string[] args, string outputFileName)
        {
            var result    = new ProcessExitInfo();
            var arguments = $"/c {appName} {string.Join(" ", args)} > {outputFileName}";
            var process   = new Process
            {
                StartInfo =
                {
                    FileName               = "cmd",
                    Arguments              = arguments,
                    CreateNoWindow         = false,
                    UseShellExecute        = false,
                    RedirectStandardOutput = false,
                    RedirectStandardError  = true
                }
            };

            process.Start();
            result.ProcessId = process.Id;
            HandleProcessExit(process, result);
            result.Errors = process.StandardError.ReadToEnd();
            process.Close();
            return(result);
        }