Пример #1
0
        /// <summary>
        /// Starts to run a command as external process with 'cmd.exe' (Windows) or '/bin/bash' (Linux/macOS)
        /// </summary>
        /// <param name="command">The command to run</param>
        /// <param name="workingDirectory">The working directory</param>
        /// <param name="onExited">The action to run when the process was exited (Exited event)</param>
        /// <param name="onOutputDataReceived">The action to run when an output message was received (OutputDataReceived event)</param>
        /// <param name="onErrorDataReceived">The action to run when an error message was received (ErrorDataReceived event)</param>
        /// <param name="captureOutput">true to capture output (standard output and error output)</param>
        /// <returns></returns>
        /// <remarks>
        /// Remember assign execution permisions to the file (sudo chmod 777 'filename') while running on Linux/macOS
        /// </remarks>
        public static Info Start(string command, string workingDirectory = null, Action <object, EventArgs> onExited = null, Action <object, DataReceivedEventArgs> onOutputDataReceived = null, Action <object, DataReceivedEventArgs> onErrorDataReceived = null, bool captureOutput = false)
        {
            var arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                                ? $"/c \"{command.Replace("\"", "\"\"\"")}\""
                                : $"-c \"{command.Replace("\"", "\\\"")}\"";

            return(ExternalProcess.Start(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd.exe" : "/bin/bash", arguments, workingDirectory, onExited, onOutputDataReceived, onErrorDataReceived, captureOutput));
        }
Пример #2
0
 /// <summary>
 /// Kills an external process that specified by identity
 /// </summary>
 /// <param name="processID">The integer that presents the identity of a process that to be killed</param>
 /// <param name="tryToClose">The action to try to close the process before the process be killed</param>
 /// <param name="onKilled">The action to run when process was killed</param>
 /// <param name="onError">The action to run when got error</param>
 public static void Kill(int processID, Action <Process> tryToClose = null, Action <Process> onKilled = null, Action <Exception> onError = null)
 {
     try
     {
         using (var process = Process.GetProcessById(processID))
         {
             ExternalProcess.Kill(process, tryToClose, onKilled, onError);
         }
     }
     catch (Exception ex)
     {
         onError?.Invoke(ex);
     }
 }
Пример #3
0
 /// <summary>
 /// Stops an external process
 /// </summary>
 /// <param name="info">The information</param>
 /// <param name="onCompleted">The action to run when completed</param>
 /// <param name="onError">The action to run when got error</param>
 public static void Stop(Info info, Action <Info> onCompleted = null, Action <Exception> onError = null)
 {
     if (info == null || info.Process == null || info.Process.HasExited)
     {
         try
         {
             info?.Process?.Dispose();
             onCompleted?.Invoke(info);
         }
         catch (Exception ex)
         {
             onError?.Invoke(ex);
         }
     }
     else
     {
         ExternalProcess.Kill(
             info.Process,
             process =>
         {
             info.Process.StandardInput.WriteLine("exit");
             info.Process.StandardInput.Close();
             info.Process.WaitForExit(456);
             info.Process.Refresh();
         },
             process =>
         {
             try
             {
                 info.ExitCode = info.Process.ExitCode;
                 info.ExitTime = info.Process.ExitTime;
             }
             catch (Exception ex)
             {
                 onError?.Invoke(ex);
             }
             try
             {
                 info.Process.Dispose();
             }
             catch (Exception ex)
             {
                 onError?.Invoke(ex);
             }
             onCompleted?.Invoke(info);
         },
             onError
             );
     }
 }
Пример #4
0
 /// <summary>
 /// Starts to run an external process directly
 /// </summary>
 /// <param name="filePath">The absolute path to the file of external process</param>
 /// <param name="arguments">The arguments</param>
 /// <param name="onExited">The action to run when the process was exited (Exited event)</param>
 /// <param name="onDataReceived">The method to handle the data receive events (include OutputDataReceived and ErrorDataReceived events)</param>
 /// <returns></returns>
 /// <remarks>
 /// Remember assign execution permisions to the file (sudo chmod 777 'filename') while running on Linux/macOS
 /// </remarks>
 public static Info Start(string filePath, string arguments, Action <object, EventArgs> onExited, Action <object, DataReceivedEventArgs> onDataReceived = null)
 => ExternalProcess.Start(filePath, arguments, null, onExited, onDataReceived, onDataReceived, false);