Last active
October 22, 2021 17:17
-
-
Save nenoNaninu/bb1615143a1876742df360d970462340 to your computer and use it in GitHub Desktop.
Makuhiki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// コンソールアプリ、別のスレッドでなんか動いていても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