C# 控制台应用程序:防止打印 Control-C?
我有一个控制台应用程序,我想捕获 Control-C 并正常关闭。
我有以下代码:
Console.CancelKeyPress += new ConsoleCancelEventHandler((o, e) =>
{
Logger.Log("Control+C hit. Shutting down.");
resetEvent.Set();
});
输出窗口显示:
6/16/2010 3:24:34 PM:按 Control+C。正在关闭。
^C
有没有办法防止 control-c 字符 ^C
出现?这不是什么大事,但出于某种原因我会很关注它,因为我就是这样的肛门。
I have a console app, and I want to capture Control-C and shutdown gracefully.
I have the following code:
Console.CancelKeyPress += new ConsoleCancelEventHandler((o, e) =>
{
Logger.Log("Control+C hit. Shutting down.");
resetEvent.Set();
});
And the output windows shows:
6/16/2010 3:24:34 PM: Control+C hit. Shutting down.
^C
Is there a way to prevent the control-c character ^C
from appearing? It's not a huge deal, but for some reason Ill be fixated on it because I'm anal like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为控制台应用程序,我将使用
Environment.Exit
来表示应用程序正在退出,这样可以阻止^C
被打印。您甚至可以提供特定的错误代码来表示用户按下了 CTRL+C。
Being a console application I would use
Environment.Exit
to signal that the application is exiting and this way stopping the^C
from being print.You could even provide a specific error code to symbolize that the user pressed CTRL+C.
这应该有效...
This should work...
虽然这不一定是您想要实现的解决方案,但它确实会阻止显示 ^C:
While this is not necessarily a solution you want to implement, it does prevent the ^C from being displayed: