Shutdown ("Exit", "Stop") now exist
This commit is contained in:
38
RelayShared/Services/ConsoleCommandService.cs
Normal file
38
RelayShared/Services/ConsoleCommandService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace RelayShared.Services;
|
||||
|
||||
public static class ConsoleCommandService
|
||||
{
|
||||
public static CancellationTokenSource ShutdownTokenSource { get; } = new();
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (!ShutdownTokenSource.IsCancellationRequested)
|
||||
{
|
||||
var input = Console.ReadLine();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
continue;
|
||||
|
||||
HandleCommand(input.Trim().ToLower());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void HandleCommand(string command)
|
||||
{
|
||||
switch (command)
|
||||
{
|
||||
case "exit":
|
||||
case "stop":
|
||||
Console.WriteLine("Shutting down...");
|
||||
ShutdownTokenSource.Cancel();
|
||||
break;
|
||||
|
||||
default:
|
||||
Console.WriteLine($"Unknown command: {command}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user