Пример #1
0
 public ProcessResult(DosContext dosContext, string str, int exitCode)
 {
     Output   = str;
     Success  = exitCode == 0 || (dosContext.AcceptBorkedExitCodes && (exitCode == 8 || exitCode == -1));
     ExitCode = exitCode;
 }
Пример #2
0
        private ProcessResult AsyncProcess(DosContext c)
        {
            try
            {
                using (var process = new Process())
                {
                    foreach (var kv in c.Environment)
                    {
#if NETFX_45
                        process.StartInfo.EnvironmentVariables.Add(kv.Key, kv.Value);
#else
                        process.StartInfo.Environment.Add(kv);
#endif
                    }
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.FileName               = c.Exe;
                    process.StartInfo.Arguments              = c.Args;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.RedirectStandardInput  = c.RedirectStandardInput;
                    process.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                    process.StartInfo.WorkingDirectory       = new FileInfo(c.Exe).DirectoryName;

                    var output = new StringBuilder();
                    var error  = new StringBuilder();

                    process.OutputDataReceived += (_, e) => c.Logger?.Invoke(e.Data);
                    process.ErrorDataReceived  += (_, e) => c.ErrorLogger?.Invoke(e.Data);
                    process.Exited             += (_, __) => c.OnProcessEnded?.Invoke();

                    process.Start();

                    c.OnProcessCreated?.Invoke(process);

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    var s             = DateTime.UtcNow;
                    var insideTimeOut = true;

                    do
                    {
                        if (process.WaitForExit(10))
                        {
                            return(new ProcessResult(c, output + error.ToString(), process.ExitCode));
                        }

                        insideTimeOut = (c.TimeOut == TimeSpan.MaxValue || s.AddMilliseconds(c.TimeOut.TotalMilliseconds) > DateTime.UtcNow);
                    } while (!c.Cancelled && insideTimeOut);

                    if (c.Cancelled)
                    {
                        try
                        {
                            if (!c.RedirectStandardInput)
                            {
                                process.Kill();
                            }
                            else
                            {
                                TryCtrlC(process);
                            }
                        }
                        catch { }
                    }

                    return(new ProcessResult(c, output + error.ToString(), process.ExitCode));
                }
            }
            catch (Exception e)
            {
                throw new Exception("DOS error with: " + c + " " + e.Message, e);
            }
        }
Пример #3
0
 public Task <ProcessResult> CmdAsync(DosContext dosContext)
 {
     return(new Task <ProcessResult>(() => AsyncProcess(dosContext)));
 }