我有以下的代码,试图在控制台应用程序中捕获Ctrl+C:
/// <summary>
/// A driver program for testing
/// </summary>
/// <param name="args">Arguments to the program</param>
static void Main(string[] args)
{
var program = new Program();
Console.Clear();
Console.TreatControlCAsInput = false;
Console.CancelKeyPress += program.OnCancelKeyPress;
program.Run(args.FirstOrDefault() ?? "3.26.200.125");
Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
/// <summary>
/// Called when [cancel key press].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.ConsoleCancelEventArgs"/> instance containing the event data.</param>
internal void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
this.Continue = false;
e.Cancel = true;
}
我已经查看了这里和这里的问题,但出现了一个问题,当我按下Control+C时,Visual Studio 2010不会进入我的处理程序,在调试器中只会显示“源代码不可用”的屏幕,并有继续调试的机会,仅此而已。有人知道为什么我没有进入处理程序吗?我确信我只是错过了一些简单的东西。