aboutsummaryrefslogtreecommitdiff
path: root/src/Constants/ConsoleCancelToken.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Constants/ConsoleCancelToken.cs')
-rw-r--r--src/Constants/ConsoleCancelToken.cs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Constants/ConsoleCancelToken.cs b/src/Constants/ConsoleCancelToken.cs
new file mode 100644
index 0000000..cc25feb
--- /dev/null
+++ b/src/Constants/ConsoleCancelToken.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Threading;
+
+namespace VNLib.Tools.Build.Executor.Constants
+{
+ internal sealed class ConsoleCancelToken : IDisposable
+ {
+ private readonly CancellationTokenSource _cts = new();
+
+ public CancellationToken Token => _cts.Token;
+
+ public ConsoleCancelToken() => Console.CancelKeyPress += OnCancel;
+
+ private void OnCancel(object? sender, ConsoleCancelEventArgs e)
+ {
+ _cts.Cancel();
+ e.Cancel = true;
+ }
+
+ public void Dispose()
+ {
+ //Unsubscribe from event
+ Console.CancelKeyPress -= OnCancel;
+ _cts.Dispose();
+
+ GC.SuppressFinalize(this);
+ }
+ }
+} \ No newline at end of file