From 2a1cd4fda8a4a8e649910d16b4dfa1ce7ae63543 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 12 May 2023 09:24:40 +0800 Subject: *misc --- .../CsvHelper.DocsGenerator/ConsoleHost.cs | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 ThirdParty/CsvHelper-master/docs-src/CsvHelper.DocsGenerator/ConsoleHost.cs (limited to 'ThirdParty/CsvHelper-master/docs-src/CsvHelper.DocsGenerator/ConsoleHost.cs') diff --git a/ThirdParty/CsvHelper-master/docs-src/CsvHelper.DocsGenerator/ConsoleHost.cs b/ThirdParty/CsvHelper-master/docs-src/CsvHelper.DocsGenerator/ConsoleHost.cs new file mode 100644 index 0000000..c7c3bc1 --- /dev/null +++ b/ThirdParty/CsvHelper-master/docs-src/CsvHelper.DocsGenerator/ConsoleHost.cs @@ -0,0 +1,117 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace CsvHelper.DocsGenerator +{ + public static class ConsoleHost + { + /// + /// Block the calling thread until shutdown is triggered via Ctrl+C or SIGTERM. + /// + public static void WaitForShutdown() + { + WaitForShutdownAsync().GetAwaiter().GetResult(); + } + + /// + /// Returns a Task that completes when shutdown is triggered via the given token, Ctrl+C or SIGTERM. + /// + /// The token to trigger shutdown. + public static async Task WaitForShutdownAsync(CancellationToken token = default(CancellationToken)) + { + var done = new ManualResetEventSlim(false); + using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token)) + { + AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: string.Empty); + await WaitForTokenShutdownAsync(cts.Token); + done.Set(); + } + } + + /// + /// Runs an application and block the calling thread until host shutdown. + /// + /// The to run. + public static void Wait() + { + WaitAsync().GetAwaiter().GetResult(); + } + + /// + /// Runs an application and returns a Task that only completes when the token is triggered or shutdown is triggered. + /// + /// The to run. + /// The token to trigger shutdown. + public static async Task WaitAsync(CancellationToken token = default(CancellationToken)) + { + //Wait for the token shutdown if it can be cancelled + if (token.CanBeCanceled) + { + await WaitAsync(token, shutdownMessage: null); + return; + } + + //If token cannot be cancelled, attach Ctrl+C and SIGTERN shutdown + var done = new ManualResetEventSlim(false); + using (var cts = new CancellationTokenSource()) + { + AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: "Application is shutting down..."); + await WaitAsync(cts.Token, "Application running. Press Ctrl+C to shut down."); + done.Set(); + } + } + + private static async Task WaitAsync(CancellationToken token, string shutdownMessage) + { + if (!string.IsNullOrEmpty(shutdownMessage)) + { + Console.WriteLine(shutdownMessage); + } + + await WaitForTokenShutdownAsync(token); + } + + private static void AttachCtrlcSigtermShutdown(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage) + { + Action ShutDown = () => + { + if (!cts.IsCancellationRequested) + { + if (!string.IsNullOrWhiteSpace(shutdownMessage)) + { + Console.WriteLine(shutdownMessage); + } + + try + { + cts.Cancel(); + } + catch (ObjectDisposedException) { } + } + + // Wait on the given reset event + resetEvent.Wait(); + }; + + AppDomain.CurrentDomain.ProcessExit += delegate { ShutDown(); }; + Console.CancelKeyPress += (sender, eventArgs) => + { + ShutDown(); + //Don't terminate the process immediately, wait for the Main thread to exit gracefully. + eventArgs.Cancel = true; + }; + } + + private static async Task WaitForTokenShutdownAsync(CancellationToken token) + { + var waitForStop = new TaskCompletionSource(); + token.Register(obj => + { + var tcs = (TaskCompletionSource)obj; + tcs.TrySetResult(null); + }, waitForStop); + await waitForStop.Task; + } + } +} -- cgit v1.1-26-g67d0