Пример #1
0
        public void Kill()
        {
            if (_outputThread != null)
            {
                if (_outputThread.IsAlive)
                {
                    _outputThread.Abort();
                }
                _outputThread = null;
            }

            if (_errorThread != null)
            {
                if (_errorThread.IsAlive)
                {
                    _errorThread.Abort();
                }
                _errorThread = null;
            }

            if (_proc != null)
            {
                if (!_proc.HasExited)
                {
                    _proc.Kill();
                }
                _proc = null;
            }
        }
Пример #2
0
        private int DoCapture(string fileName, string args, string workingDir)
        {
            Kill();

            _proc = new Process();
            ProcessStartInfo info = new ProcessStartInfo(fileName, args);

            info.UseShellExecute        = false;
            info.RedirectStandardOutput = _captureOutput;
            info.RedirectStandardError  = _captureError;
            info.CreateNoWindow         = true;
            info.WorkingDirectory       = workingDir;
            _proc.StartInfo             = info;
            if (!_proc.Start())
            {
                throw new ProcessRunnerException(string.Format("Failed to start process '{0}'.", fileName));
            }

            lock (_outputLines)
            {
                _outputLines.Clear();
            }

            _outputThread = null;
            _errorThread  = null;
            if (_captureOutput)
            {
                _outputThread = new ProcessRunnerThread(this, false, _proc);
                _outputThread.Start("StdOut Capture Thread");
            }
            if (_captureError)
            {
                _errorThread = new ProcessRunnerThread(this, true, _proc);
                _errorThread.Start("StdErr Capture Thread");
            }

            string line;

            // Grabs the lines while the process runs.
            while ((_outputThread != null && _outputThread.IsAlive) ||
                   (_errorThread != null && _errorThread.IsAlive))
            {
                lock (_outputLines)
                {
                    while (_outputLines.Count > 0)
                    {
                        line = (string)_outputLines[0];
                        if (line != null)
                        {
                            if (_output != null)
                            {
                                _output.WriteLine(line);
                            }
                            _outputLines.RemoveAt(0);
                        }
                    }
                }
                System.Threading.Thread.Sleep(100);
            }

            // Process has finished. Grab the rest of the lines.
            lock (_outputLines)
            {
                while (_outputLines.Count > 0)
                {
                    line = (string)_outputLines[0];
                    if (line != null)
                    {
                        if (_output != null)
                        {
                            _output.WriteLine(line);
                        }
                        _outputLines.RemoveAt(0);
                    }
                }
            }

            while (!_proc.HasExited)
            {
                System.Threading.Thread.Sleep(100);
            }

            int exitCode = _proc.ExitCode;

            _proc = null;
            return(exitCode);
        }
Пример #3
0
        public void Kill()
        {
            if (_outputThread != null)
            {
                if (_outputThread.IsAlive) _outputThread.Abort();
                _outputThread = null;
            }

            if (_errorThread != null)
            {
                if (_errorThread.IsAlive) _errorThread.Abort();
                _errorThread = null;
            }
        }
Пример #4
0
        private int DoCapture(string fileName, string args, string workingDir)
        {
            Kill();

            using (var proc = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo(fileName, args);
                info.UseShellExecute = false;
                info.RedirectStandardOutput = _captureOutput;
                info.RedirectStandardError = _captureError;
                info.CreateNoWindow = true;
                info.WorkingDirectory = workingDir;
                proc.StartInfo = info;
                if (!proc.Start()) throw new ProcessRunnerException(string.Format("Failed to start process '{0}'.", fileName));

                lock (_outputLines)
                {
                    _outputLines.Clear();
                }

                _outputThread = null;
                _errorThread = null;
                if (_captureOutput)
                {
                    _outputThread = new ProcessRunnerThread(this, false, proc);
                    _outputThread.Start("StdOut Capture Thread");
                }
                if (_captureError)
                {
                    _errorThread = new ProcessRunnerThread(this, true, proc);
                    _errorThread.Start("StdErr Capture Thread");
                }

                string line;

                // Grabs the lines while the process runs.
                while ((_outputThread != null && _outputThread.IsAlive) ||
                    (_errorThread != null && _errorThread.IsAlive))
                {
                    lock (_outputLines)
                    {
                        while (_outputLines.Count > 0)
                        {
                            line = (string)_outputLines[0];
                            if (line != null)
                            {
                                if (_output != null) _output.WriteLine(line);
                                _outputLines.RemoveAt(0);
                            }
                        }
                    }
                    System.Threading.Thread.Sleep(100);
                }

                // Process has finished. Grab the rest of the lines.
                lock (_outputLines)
                {
                    while (_outputLines.Count > 0)
                    {
                        line = (string)_outputLines[0];
                        if (line != null)
                        {
                            if (_output != null) _output.WriteLine(line);
                            _outputLines.RemoveAt(0);
                        }
                    }
                }

                while (!proc.HasExited)
                {
                    System.Threading.Thread.Sleep(100);
                }

                int exitCode = proc.ExitCode;
                return exitCode;
            }
        }