windows 7中的捕获控制台退出C#
有人知道如何在windows中的c#控制台中对ctrl+c事件做出反应吗 这个问题:说明了如何做,但我已经尝试过了,它只在用户单击控制台窗口顶部的close X时捕获事件 当用户键入ctrl+c时不会发生任何事情,调试时它甚至不会命中处理程序 谢谢 这是我的密码windows 7中的捕获控制台退出C#,c#,.net,events,console,exit,C#,.net,Events,Console,Exit,有人知道如何在windows中的c#控制台中对ctrl+c事件做出反应吗 这个问题:说明了如何做,但我已经尝试过了,它只在用户单击控制台窗口顶部的close X时捕获事件 当用户键入ctrl+c时不会发生任何事情,调试时它甚至不会命中处理程序 谢谢 这是我的密码 namespace EventCloseConsole { using System.Runtime.InteropServices; using System; class Program {
namespace EventCloseConsole
{
using System.Runtime.InteropServices;
using System;
class Program
{
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler;
enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static bool Handler(CtrlType sig)
{
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
Console.WriteLine("Closing");
System.Threading.Thread.Sleep(500);
return false;
default:
return true;
}
}
static void Main(string[] args)
{
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
Console.ReadLine();
}
}
}
您需要将Console.CancelKeyPress事件连接到处理程序。这是一篇关于这个主题的好文章 这在Windows 7上对我很有效。用x按钮关闭
秘密在于可变静态控制台eventdelegate\u d
private static void Main(string[] args)
{
ConsoleEventHooker.Closed += ConsoleEventHooker_Closed;
}
static void ConsoleHooker_Closed(object sender, EventArgs e)
{
}
ConsoleEventHooker.cs
namespace System
{
internal static class ConsoleEventHooker
{
private static bool _initedHooker;
private static EventHandler _closed;
private static EventHandler _shutdown;
private static ConsoleEventDelegate _d;
public static event EventHandler Closed
{
add
{
Init();
_closed += value;
}
remove { _closed -= value; }
}
public static event EventHandler Shutdown
{
add
{
Init();
_shutdown += value;
}
remove { _shutdown -= value; }
}
private static void Init()
{
if (_initedHooker) return;
_initedHooker = true;
_d = ConsoleEventCallback;
SetConsoleCtrlHandler(_d, true);
}
private static bool ConsoleEventCallback(CtrlTypes eventType)
{
if (eventType == CtrlTypes.CTRL_CLOSE_EVENT)
{
if(_closed != null) _closed(null,new EventArgs());
}
if (eventType == CtrlTypes.CTRL_SHUTDOWN_EVENT)
{
if (_shutdown != null) _shutdown(null, new EventArgs());
}
return false;
}
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
delegate bool ConsoleEventDelegate(CtrlTypes ctrlType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
}
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
调试器遇到了阻碍,它也会查找Ctrl+C。用Ctrl+F5启动程序以测试这一点。不幸的是,这对我不起作用,我将代码直接复制并粘贴到一个新的控制台应用程序中。我注意到这是在2006年写的,可能是因为我使用的是Windows7吗?这应该是正确的。你能在你的问题中添加你尝试过的代码吗。只需添加Console.CancelKeyPress+=新的ConsoleCancelEventHandler(myHandler);到主方法,该链接现在已断开。改为添加了msdn链接。