-1

I've implememted a small Console Application that checks how long a programm is running. I then tried to run the application and everything is working fine. However then i hit (by accident) the "pause" key on my Keyboard and the programm stopped executing.

Is there a way to handle this event in a Console Application to suppress this pausing?

Update:

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Thread.Sleep(2000);
            var p = Process.GetProcessesByName("wineks");
            if (p != null)
            {                   
                Console.WriteLine("Found Process. Close it please");
                Console.ReadKey();
            }
        }
    }
}

That is basically my code. It is only asking the user to close a specific process. If I know hit the Pause Button on my Keyboard before I see the message, the message will never appear because the application freezes and seems like paused. From browsing in the Internet I know that the key I press has the Name Pause and the key is sending some kind of Event or Signal to the Console.

Brezelmann
  • 359
  • 3
  • 16
  • 1
    No, there is nothing you can do about it in your code. You've simply discovered that the console mode project template was not the right way to implement this feature. Live and learn, a good way to generate notifications is with the NotifyIcon class in a windows form app. – Hans Passant Mar 12 '19 at 16:30
  • I am not really interested. That is just a Server thing that is requested by my higher ups not me (I personally would have used either a Windows Service or Winforms for that). However thats not what I was asked about therefore I am not changing it. My Question is just: How do I stop the Pause Key from pausing my console Application. – Brezelmann Mar 13 '19 at 07:15

1 Answers1

0

Actually the Console does not have a way to raise KeyPress events , you can however try some looped approach to handle any key press done accidentally . Refer to this stackoverflow question here

Try inserting these lines before GetProcessByName

if (Console.KeyAvailable) 
{
    if (ConsoleKey.Pause == Console.ReadKey().Key)
     continue; 
}
Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34
  • I have something quite similar to the code in your example. My Application runs a While true loop while asking Console for the next Key with Console.ReadKey(). However this still doesn't stop the Pause from happening when im pressing the Pause key – Brezelmann Mar 12 '19 at 14:27
  • I Updated my question with the code that i am having trouble with. – Brezelmann Mar 12 '19 at 15:26
  • I Inserted the Code you've shown me. However it doesn't work. The Programm still pauses. Even if I set a breakpoint directly at the second if. It doesn't get hit. It seems like the Console is not registering the Pause key as a Keypress but a Signal instead. Is there a way to "catch" or ignore this signal? – Brezelmann Mar 13 '19 at 07:23