C# 控制台应用程序赢得';t在给定ctrl+时退出;调试模式下的c
在Release中运行以下命令时,按CTRL+C键可成功终止应用程序 在调试中运行时,按下C# 控制台应用程序赢得';t在给定ctrl+时退出;调试模式下的c,c#,C#,在Release中运行以下命令时,按CTRL+C键可成功终止应用程序 在调试中运行时,按下CTRL+C将挂起,而下面的循环将挂起 为什么??有办法解决这个问题吗 static void Main(string[] args) { while (true) { // Press CTRL + C... // When running in Release, the app closes down // When running in
CTRL+C
将挂起,而下面的循环将挂起
为什么??有办法解决这个问题吗
static void Main(string[] args)
{
while (true)
{
// Press CTRL + C...
// When running in Release, the app closes down
// When running in Debug, it hangs in here
}
}
实现这一点的方法之一是使用
当控件修改器键(Ctrl)
和
ConsoleKey.C
console键(C)或断开键被按下
同时(Ctrl+C或Ctrl+Break)
当用户按下Ctrl+C
或Ctrl+Break
时,CancelKeyPress
事件被激发,应用程序的ConsoleCancelEventHandler
事件
处理程序被执行。事件处理程序被传递一个
ConsoleCancelEventArgs
object
示例
private static bool keepRunning = true;
public static void Main(string[] args)
{
Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
keepRunning = false;
};
while (keepRunning)
{
// Do stuff
}
Console.WriteLine("exited gracefully");
}