Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep a console open until CancelKeyPress event is fired?

Tags:

c#

.net

console

What is the best way to keep a console application open as long as the CancelKeyPress event has not been fired?

I would prefer to not use Console.Read or Console.ReadLine as I do not want to accept input. I just want to enable the underlying application to print to the console event details as they are fired. Then once the CancelKeyPress event is fired I want to gracefully shut down the application.

like image 402
Eric Schoonover Avatar asked Oct 14 '08 23:10

Eric Schoonover


People also ask

How do I stop the console app from closing?

The first solution is to run the application without debugging by using Ctrl+F5 instead of just F5. The console window will remain open when the program has finished.

How do I keep the console window open?

To keep the console window open in Visual Studio without using the Console. ReadLine() method, you should run the application without debug mode by pressing Ctrl+F5 or by clicking on the menu Debug > Start without Debugging option. This way the application remains active below until the user presses a key.

How do I keep a program from running in C#?

Keep Console Open With the Ctrl + F5 Shortcut in C# The best approach for keeping our console window open after the execution of code is to run it with the Ctrl + F5 shortcut of the Microsoft Visual Studio IDE. Our program runs in debug mode when we run our code by clicking the start button in the Visual Studio IDE.

How do you pause a console application?

Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console. Readline() or ReadKey() functions. Save this answer.


2 Answers

I'm assuming that "gracefully shut down the application" is the part you are struggling with here. Otherwise your application will automatically exit on ctrl-c. You should change the title.

Here's a quick demo of what I think you need. It could be refined a bit more with use of locking and Monitors for notification. I'm not sure exactly what you need though, so I'll just pose this...

class Program
{

    private static volatile bool _s_stop = false;

    public static void Main(string[] args)
    {
        Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
        while (!_s_stop)
        {
            /* put real logic here */
            Console.WriteLine("still running at {0}", DateTime.Now);
            Thread.Sleep(3000);
        }
        Console.WriteLine("Graceful shut down code here...");

        //don't leave this...  demonstration purposes only...
        Console.ReadLine();
    }

    static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
        //you have 2 options here, leave e.Cancel set to false and just handle any
        //graceful shutdown that you can while in here, or set a flag to notify the other
        //thread at the next check that it's to shut down.  I'll do the 2nd option
        e.Cancel = true;
        _s_stop = true;
        Console.WriteLine("CancelKeyPress fired...");
    }

}

The _s_stop boolean should be declared volatile or an overly-ambitious optimizer might cause the program to loop infinitely.

like image 112
TheSoftwareJedi Avatar answered Sep 21 '22 14:09

TheSoftwareJedi


There is already a handler bound to CancelKeyPress that terminates your application, the only reason to hook to it is if you want to intercept the event and prevent the app from closing.

In your situation, just put your app into an infinite loop, and let the built in event handler kill it. You may want to look into using something like Wait(1) or a background process to prevent it from using tons of CPU while doing nothing.

like image 35
FlySwat Avatar answered Sep 21 '22 14:09

FlySwat