Esempio n. 1
0
        public static async Task <int> Start(string programPath, string arguments, string workingDirectory, Action <string, bool> outputDelegate, CancellationToken?cancelToken = null)
        {
            var action = new ProcessAction {
                ProgramPath = programPath, Arguments = arguments, WorkingDirectory = workingDirectory
            };

            return(await Start(action, outputDelegate, cancelToken));
        }
Esempio n. 2
0
        public static async Task <int> Start(ProcessAction action, Action <string, bool> outputDelegate, CancellationToken?cancelToken = null)
        {
            if (string.IsNullOrWhiteSpace(action.ProgramPath))
            {
                throw new NullReferenceException($"One of the action arguments is null or empty");
            }

            if (!File.Exists(action.ProgramPath))
            {
                throw new FileNotFoundException($"Cannot find {action.ProgramPath}");
            }

            if (_runningProcesses == null)
            {
                _runningProcesses = new ConcurrentDictionary <Guid, Process>();
            }

            var processStartInfo = new ProcessStartInfo();

            processStartInfo.FileName               = action.ProgramPath;
            processStartInfo.WorkingDirectory       = action.WorkingDirectory;
            processStartInfo.Arguments              = action.Arguments;
            processStartInfo.UseShellExecute        = false;
            processStartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            processStartInfo.RedirectStandardError  = true;
            processStartInfo.RedirectStandardOutput = true;

            using (var p = Process.Start(processStartInfo))
            {
                p.BeginErrorReadLine();
                p.BeginOutputReadLine();
                p.OutputDataReceived += new DataReceivedEventHandler((sender, e) => { if (!string.IsNullOrWhiteSpace(e.Data))
                                                                                      {
                                                                                          outputDelegate(e.Data, false);
                                                                                      }
                                                                     });
                p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) => { if (!string.IsNullOrWhiteSpace(e.Data))
                                                                                     {
                                                                                         outputDelegate(e.Data, true);
                                                                                     }
                                                                    });

                if (!_runningProcesses.TryAdd(action.Id, p))
                {
                    throw new Exception("Could not persist process to memory");
                }

                //wait idenfinitely for the process to finish
                while (!p.HasExited)
                {
                    await Task.Delay(10 * 1000);
                }

                return(p.ExitCode);
            }
        }
Esempio n. 3
0
        public static void Cancel(ProcessAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (!_runningProcesses.TryGetValue(action.Id, out Process runningProcess))
            {
                throw new Exception($"Failed to find process that exist with Id {action.Id}");
            }

            try
            {
                runningProcess.Kill();
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to kill process with id {action.Id}", ex);
            }
        }
Esempio n. 4
0
        public static bool TryCancel(ProcessAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            if (!_runningProcesses.TryGetValue(action.Id, out Process runningProcess))
            {
                return(false);
            }

            try
            {
                runningProcess.Kill();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }