-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignals.cs
32 lines (29 loc) · 914 Bytes
/
Signals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Threading;
internal static class Signals
{
internal static void AttachCtrlCSigtermShutdown(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage)
{
void ShutDown()
{
if (!cts.IsCancellationRequested)
{
if (!string.IsNullOrWhiteSpace(shutdownMessage))
Console.WriteLine(shutdownMessage);
try
{
cts.Cancel();
}
catch (ObjectDisposedException) { }
}
resetEvent.Wait();
Thread.Sleep(1000);
}
AppDomain.CurrentDomain.ProcessExit += delegate { ShutDown(); };
Console.CancelKeyPress += (sender, eventArgs) =>
{
ShutDown();
eventArgs.Cancel = true;
};
}
}