Browse Source

graceful termination in windows (#600)

finitereality/replica-status-colors
areller 6 years ago
committed by GitHub
parent
commit
7c66037d84
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      src/Microsoft.Tye.Core/ProcessUtil.cs

28
src/Microsoft.Tye.Core/ProcessUtil.cs

@ -14,9 +14,15 @@ namespace Microsoft.Tye
{
public static class ProcessUtil
{
#region Native Methods
[DllImport("libc", SetLastError = true, EntryPoint = "kill")]
private static extern int sys_kill(int pid, int sig);
#endregion
private static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static async Task<ProcessResult> RunAsync(
string filename,
string arguments,
@ -38,7 +44,8 @@ namespace Microsoft.Tye
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
CreateNoWindow = !IsWindows,
WindowStyle = ProcessWindowStyle.Hidden
},
EnableRaisingEvents = true
};
@ -115,20 +122,23 @@ namespace Microsoft.Tye
if (result == cancelledTcs.Task)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!IsWindows)
{
sys_kill(process.Id, sig: 2); // SIGINT
var cancel = new CancellationTokenSource();
await Task.WhenAny(processLifetimeTask.Task, Task.Delay(TimeSpan.FromSeconds(5), cancel.Token));
cancel.Cancel();
}
else
{
if (!process.CloseMainWindow())
{
process.Kill();
}
}
if (!process.HasExited)
{
process.CloseMainWindow();
var cancel = new CancellationTokenSource();
await Task.WhenAny(processLifetimeTask.Task, Task.Delay(TimeSpan.FromSeconds(5), cancel.Token));
cancel.Cancel();
if (!process.HasExited)
{

Loading…
Cancel
Save