Beispiel #1
0
        public static bool RunAndReturnStatus(string exeName, string args, string folder, LogBase log)
        {
            log.Info("Starting: ", Color.DarkGray);
            log.Info(exeName, Color.PaleGreen);
            log.Info($" {args}", Color.GhostWhite);
            log.InfoL($" (in {folder})", Color.Gray);

            using var process = new Process
                  {
                      StartInfo = new ProcessStartInfo
                      {
                          FileName               = exeName,
                          Arguments              = args,
                          UseShellExecute        = false,
                          RedirectStandardOutput = true,
                          RedirectStandardError  = true,
                          WorkingDirectory       = folder
                      }
                  };
            var lockObj = new object();

            process.OutputDataReceived += (sender, eventArgs) =>
            {
                if (eventArgs.Data == null)
                {
                    return;
                }
                lock (lockObj)
                    log.InfoL($"[out] {eventArgs.Data}", Color.PaleGreen);
            };
            process.ErrorDataReceived += (sender, eventArgs) =>
            {
                if (eventArgs.Data == null)
                {
                    return;
                }
                lock (lockObj)
                    log.InfoL($"[err] {eventArgs.Data}", Color.OrangeRed);
            };
            log.Newline();
            log.ClearPrependers();
            process.Start();
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
            process.WaitForExit();
            log.RestorePrependers();
            log.Newline();

            if (process.ExitCode != 0)
            {
                return(false);
            }
            return(true);
        }