C# 在 CancelKeyPress 事件被触发之前,如何保持控制台打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/203246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I keep a console open until CancelKeyPress event is fired?
提问by Eric Schoonover
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 事件被触发,我想优雅地关闭应用程序。
采纳答案by TheSoftwareJedi
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,否则过于雄心勃勃的优化器可能会导致程序无限循环。
回答by FlySwat
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。
回答by Mattio
This may be what you're looking for:
这可能是您正在寻找的:
http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx
http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx
回答by TheSoftwareJedi
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,否则过于雄心勃勃的优化器可能会导致程序无限循环。
回答by victoromondy
Simply run your program or codes without debugging, on your keyboard key in CTRL + f5instead of F5(debugging).
只需在键盘上输入CTRL + f5而不是 F5(调试)即可运行您的程序或代码而无需调试。