|
|
@@ -1,14 +1,16 @@
|
|
|
using System;
|
|
|
using System.Diagnostics;
|
|
|
using System.Runtime.InteropServices;
|
|
|
+using System.Threading.Tasks;
|
|
|
using Path = System.IO.Path;
|
|
|
|
|
|
namespace ET
|
|
|
{
|
|
|
public static class ProcessHelper
|
|
|
{
|
|
|
- public static Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
|
|
|
+ public static Process Run(string exe, string arguments, string workingDirectory = ".")
|
|
|
{
|
|
|
+ //Log.Debug($"Process Run exe:{exe} ,arguments:{arguments} ,workingDirectory:{workingDirectory}");
|
|
|
try
|
|
|
{
|
|
|
bool redirectStandardOutput = true;
|
|
|
@@ -20,14 +22,6 @@ namespace ET
|
|
|
redirectStandardError = false;
|
|
|
useShellExecute = true;
|
|
|
}
|
|
|
-
|
|
|
- if (waitExit)
|
|
|
- {
|
|
|
- redirectStandardOutput = true;
|
|
|
- redirectStandardError = true;
|
|
|
- useShellExecute = false;
|
|
|
- }
|
|
|
-
|
|
|
ProcessStartInfo info = new ProcessStartInfo
|
|
|
{
|
|
|
FileName = exe,
|
|
|
@@ -38,17 +32,10 @@ namespace ET
|
|
|
RedirectStandardOutput = redirectStandardOutput,
|
|
|
RedirectStandardError = redirectStandardError,
|
|
|
};
|
|
|
-
|
|
|
- Process process = Process.Start(info);
|
|
|
|
|
|
- if (waitExit)
|
|
|
- {
|
|
|
- process.WaitForExit();
|
|
|
- if (process.ExitCode != 0)
|
|
|
- {
|
|
|
- throw new Exception($"{process.StandardOutput.ReadToEnd()} {process.StandardError.ReadToEnd()}");
|
|
|
- }
|
|
|
- }
|
|
|
+ Process process = Process.Start(info);
|
|
|
+
|
|
|
+ WaitExitAsync(process);
|
|
|
|
|
|
return process;
|
|
|
}
|
|
|
@@ -57,5 +44,55 @@ namespace ET
|
|
|
throw new Exception($"dir: {Path.GetFullPath(workingDirectory)}, command: {exe} {arguments}", e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static async void WaitExitAsync(Process process)
|
|
|
+ {
|
|
|
+ await process.WaitForExitAsync();
|
|
|
+#if NOT_UNITY
|
|
|
+ Log.Info($"process exit, exitcode: {process.ExitCode} {process.StandardOutput.ReadToEnd()} {process.StandardError.ReadToEnd()}");
|
|
|
+#endif
|
|
|
+ }
|
|
|
+
|
|
|
+#if !NOT_UNITY
|
|
|
+ private static async Task WaitForExitAsync(this Process self)
|
|
|
+ {
|
|
|
+ if (!self.HasExited)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ self.EnableRaisingEvents = true;
|
|
|
+ }
|
|
|
+ catch (InvalidOperationException)
|
|
|
+ {
|
|
|
+ if (self.HasExited)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+
|
|
|
+ var tcs = new TaskCompletionSource<bool>();
|
|
|
+
|
|
|
+ void Handler(object s, EventArgs e) => tcs.TrySetResult(true);
|
|
|
+
|
|
|
+ self.Exited += Handler;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (self.HasExited)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ await tcs.Task;
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ self.Exited -= Handler;
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
}
|