Skip to content

Commit

Permalink
Merge pull request #180 from ptr727/keyboard-quit
Browse files Browse the repository at this point in the history
Test for input console redirection
  • Loading branch information
ptr727 authored Jun 21, 2023
2 parents ceb92eb + 475aaa6 commit 1717ddd
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions PlexCleaner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ private static int Main()
// Wait for debugger to attach
WaitForDebugger();

// Register cancel and keyboard handlers
Task consoleKeyTask = null;
if (Environment.UserInteractive)
{
Console.CancelKeyPress += CancelEventHandler;
consoleKeyTask = Task.Run(KeyPressHandler);
Console.WriteLine("Press Ctrl+C or Ctrl+Z or Ctrl+Q to exit.");
// Display version information only
if (ShowVersionInformation())
{
// Exit immediately to not print anything else
return 0;
}

// Register cancel and keyboard handlers
Console.CancelKeyPress += CancelEventHandler;
var consoleKeyTask = Task.Run(KeyPressHandler);
Console.WriteLine("Press Ctrl+C or Ctrl+Z or Ctrl+Q to exit.");

// Create default logger
CreateLogger(null);

Expand All @@ -55,7 +58,7 @@ private static int Main()

// Cancel background operations
Cancel();
consoleKeyTask?.Wait();
consoleKeyTask.Wait();

// Stop the timer
keepAwakeTimer.Stop();
Expand All @@ -70,9 +73,27 @@ private static int Main()
return exitCode;
}

private static bool ShowVersionInformation()
{
// Use the raw commandline and look for --version
if (Environment.CommandLine.Contains("--version"))
{
string appVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
Console.WriteLine(appVersion);
return true;
}
return false;
}

private static void KeyPressHandler()
{
for(;;)
// Skip if input is redirected
if (Console.IsInputRedirected)
{
return;
}

for (;;)
{
// Wait on key available or cancelled
while (!Console.KeyAvailable)
Expand Down

0 comments on commit 1717ddd

Please sign in to comment.