在解除CancelKeyPress事件之前,如何保持控制台处于打开状态?

时间:2021-06-12 16:51:11

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

只要还没有触发CancelKeyPress事件,保持控制台应用程序打开的最佳方法是什么?

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.

我宁愿不使用Console.Read或Console.ReadLine,因为我不想接受输入。我只是想让底层应用程序在触发时打印到控制台事件详细信息。然后一旦触发CancelKeyPress事件,我想优雅地关闭应用程序。

5 个解决方案

#1


10  

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.

我假设“优雅地关闭应用程序”是你在这里努力的部分。否则,您的应用程序将自动退出ctrl-c。你应该改变标题。

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.

_s_stop布尔值应该声明为volatile,或者过于雄心勃勃的优化器可能导致程序无限循环。

#2


5  

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

_s_stop布尔值应该在示例代码中声明为volatile,否则过于雄心勃勃的优化器可能导致程序无限循环。

#3


1  

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.

已经有一个绑定到CancelKeyPress的处理程序终止了你的应用程序,钩住它的唯一原因是你想拦截事件并阻止应用程序关闭。

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.

在您的情况下,只需将您的应用程序置于无限循环中,然后让内置事件处理程序终止它。您可能希望使用Wait(1)或后台进程之类的东西来防止它在什么也不做的情况下使用大量的CPU。

#4


1  

This may be what you're looking for:

这可能是您正在寻找的:

http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx

#5


0  

Simply run your program or codes without debugging, on your keyboard key in CTRL + f5 instead of F5(debugging).

只需在CTRL + f5而不是F5(调试)的键盘上运行您的程序或代码而无需调试。

#1


10  

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.

我假设“优雅地关闭应用程序”是你在这里努力的部分。否则,您的应用程序将自动退出ctrl-c。你应该改变标题。

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.

_s_stop布尔值应该声明为volatile,或者过于雄心勃勃的优化器可能导致程序无限循环。

#2


5  

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

_s_stop布尔值应该在示例代码中声明为volatile,否则过于雄心勃勃的优化器可能导致程序无限循环。

#3


1  

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.

已经有一个绑定到CancelKeyPress的处理程序终止了你的应用程序,钩住它的唯一原因是你想拦截事件并阻止应用程序关闭。

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.

在您的情况下,只需将您的应用程序置于无限循环中,然后让内置事件处理程序终止它。您可能希望使用Wait(1)或后台进程之类的东西来防止它在什么也不做的情况下使用大量的CPU。

#4


1  

This may be what you're looking for:

这可能是您正在寻找的:

http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx

#5


0  

Simply run your program or codes without debugging, on your keyboard key in CTRL + f5 instead of F5(debugging).

只需在CTRL + f5而不是F5(调试)的键盘上运行您的程序或代码而无需调试。