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

Commit

Permalink
Fix CTRL + C
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak committed Apr 14, 2021
1 parent b2efec1 commit 9560e5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/Impostor.Server/Input/ConsoleInputService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
var line = _susLine?.ReadLine(stoppingToken) ?? Console.ReadLine();
string? line;

if (_config.SusLine)
{
line = await _susLine!.ReadLineAsync(stoppingToken);
}
else
{
var task = Task.Run(Console.ReadLine, stoppingToken);
await Task.WhenAny(task, Task.Delay(Timeout.Infinite, stoppingToken));

line = task.IsCompleted ? task.GetAwaiter().GetResult() : null;
}

if (string.IsNullOrEmpty(line))
{
Expand Down
5 changes: 3 additions & 2 deletions src/Impostor.Server/Input/SusLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

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

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

switch (keyInfo.Key)
{
Expand Down

0 comments on commit 9560e5f

Please sign in to comment.