Skip to content

Instantly share code, notes, and snippets.

@nenoNaninu
Last active October 22, 2021 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nenoNaninu/bb1615143a1876742df360d970462340 to your computer and use it in GitHub Desktop.
Save nenoNaninu/bb1615143a1876742df360d970462340 to your computer and use it in GitHub Desktop.
Makuhiki
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Makuhiki;
public static class Exit
{
public static Task WaitAsync()
{
var tcs = new TaskCompletionSource();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
tcs.SetResult();
};
return tcs.Task;
}
public static void Wait()
{
using var manualResetEventSlim = new ManualResetEventSlim();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
manualResetEventSlim.Set();
};
manualResetEventSlim.Wait();
}
}
// コンソールアプリ、別のスレッドでなんか動いていてもMain()が終わるとプログラムが終了してしまう。
// なのでよく
Console.ReadLine();
// で終了するのを防いでいる。どーにかならんもんかね。
// 2案思いつた。
//その1
var tcs = new TaskCompletionSource();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
tcs.SetResult();
};
await tcs.Task;
// その2
using var manualResetEventSlim = new ManualResetEventSlim();
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
manualResetEventSlim.Set();
};
manualResetEventSlim.Wait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment