示例#1
0
        public ChoProcessResult Execute()
        {
            ChoProcessResult result = new ChoProcessResult();

            Execute(result);
            return(result);
        }
示例#2
0
        public ChoProcessResult Execute(ProcessStartInfo processStartInfo)
        {
            ChoProcessResult result = new ChoProcessResult();

            Execute(processStartInfo, result);
            return(result);
        }
示例#3
0
        private void Execute(ProcessStartInfo processStartInfo, ChoProcessResult result)
        {
            _process           = result.Process = new System.Diagnostics.Process();
            _process.StartInfo = processStartInfo;
            if (processStartInfo.RedirectStandardOutput)
            {
                _process.OutputDataReceived += (sender, e) => { RaiseOutputDataReceived(e); }
            }
            ;
            if (processStartInfo.RedirectStandardError)
            {
                _process.ErrorDataReceived += (sender, e) => { RaiseErrorDataReceived(e); }
            }
            ;

            _process.EnableRaisingEvents = true;
            _process.Start();

            if (processStartInfo.RedirectStandardOutput)
            {
                _process.BeginOutputReadLine();
            }
            if (processStartInfo.RedirectStandardError)
            {
                _process.BeginErrorReadLine();
            }

            if (Timeout > 0)
            {
                if (!_process.WaitForExit(Timeout))
                {
                    throw new ChoTimeoutException("Process timedout [{0}ms].".FormatString(Timeout));
                }
            }
            else
            {
                _process.WaitForExit();
            }

            result.ExitCode = _process.ExitCode;
            result.StdOut   = _stdout.ToString();
            result.StdErr   = _stderr.ToString();

            if (result.ExitCode != 0)
            {
                throw new ChoProcessException("Process exited with '{0}' error code.".FormatString(result.ExitCode));
            }
            else if (!result.StdErr.IsNullOrWhiteSpace())
            {
                throw new ChoProcessException(result.StdErr);
            }
        }
示例#4
0
        public ChoProcessResult ExecuteAsync()
        {
            ChoProcessResult result = new ChoProcessResult();

            Task task = Task.Factory.StartNew((r) =>
            {
                Execute(r as ChoProcessResult);
            }, result);

            result.Task = task;

            return(result);
        }
示例#5
0
        private void Execute(ChoProcessResult result)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo();

            processStartInfo.UseShellExecute = UseShellExecute;
            processStartInfo.CreateNoWindow  = true;
            processStartInfo.Arguments       = Arguments;
            processStartInfo.FileName        = FileName;
            if (!Verb.IsNullOrWhiteSpace())
            {
                processStartInfo.Verb = Verb;
            }
            processStartInfo.RedirectStandardOutput = !UseShellExecute;
            processStartInfo.RedirectStandardError  = !UseShellExecute;

            if (!WorkingDirectory.IsNullOrWhiteSpace())
            {
                processStartInfo.WorkingDirectory = WorkingDirectory;
            }
            else if (!Path.GetDirectoryName(FileName).IsNullOrWhiteSpace())
            {
                processStartInfo.WorkingDirectory = Path.GetDirectoryName(FileName);
            }

            if (!Domain.IsNullOrWhiteSpace())
            {
                processStartInfo.Domain = Domain;
            }

            if (!UserName.IsNullOrWhiteSpace())
            {
                processStartInfo.UserName = UserName;
            }

            if (Password != null)
            {
                processStartInfo.Password = Password;
            }

            Execute(processStartInfo, result);
        }