Skip to content
This repository was archived by the owner on Mar 3, 2021. It is now read-only.

Commit 115631f

Browse files
committed
Fix CTRL + C
1 parent 27f27b3 commit 115631f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Impostor.Server/Input/ConsoleInputService.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3636
{
3737
try
3838
{
39-
var line = _susLine?.ReadLine(stoppingToken) ?? Console.ReadLine();
39+
string? line;
40+
41+
if (_config.SusLine)
42+
{
43+
line = await _susLine!.ReadLineAsync(stoppingToken);
44+
}
45+
else
46+
{
47+
var task = Task.Run(Console.ReadLine, stoppingToken);
48+
await Task.WhenAny(task, Task.Delay(Timeout.Infinite, stoppingToken));
49+
50+
line = task.IsCompleted ? task.GetAwaiter().GetResult() : null;
51+
}
4052

4153
if (string.IsNullOrEmpty(line))
4254
{

src/Impostor.Server/Input/SusLine.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.InteropServices;
55
using System.Text;
66
using System.Threading;
7+
using System.Threading.Tasks;
78

89
namespace Impostor.Server.Input
910
{
@@ -29,7 +30,7 @@ public SusLine()
2930
/// </summary>
3031
/// <param name="stoppingToken"><see cref="CancellationToken"/> for a while loop.</param>
3132
/// <returns>The next line of characters from the input stream, or null if no more lines are available.</returns>
32-
public string? ReadLine(CancellationToken stoppingToken)
33+
public async ValueTask<string?> ReadLineAsync(CancellationToken stoppingToken)
3334
{
3435
while (!stoppingToken.IsCancellationRequested)
3536
{
@@ -38,7 +39,7 @@ public SusLine()
3839
continue;
3940
}
4041

41-
var keyInfo = Console.ReadKey(true);
42+
var keyInfo = await Task<ConsoleKeyInfo>.Factory.StartNew(static _ => Console.ReadKey(true), this, stoppingToken, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
4243

4344
switch (keyInfo.Key)
4445
{

0 commit comments

Comments
 (0)