2011-01-23 91 views
5

有誰知道如何對ctrl中的ctrl + c事件做出反應嗎?在Windows 7中捕獲控制檯出口C#

這個問題:Capture console exit C#說如何做到這一點,但我試過了,它只捕獲事件,當用戶點擊控制檯窗口頂部的關閉X.

當用戶鍵入ctrl + c時,沒有任何反應,它在調試時甚至不會觸發處理程序。

感謝

這裏是我的代碼

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(); 


     } 
    } 
} 
+2

調試器的方式得到,它也會查找Ctrl + C。用Ctrl + F5開始你的程序來測試它。 – 2011-01-23 15:21:26

回答

1

您需要的Console.CancelKeyPress事件連接到一個處理器。 Here是關於這個話題的一篇很棒的文章。

+0

不幸的是,這不適合我,我直接複製並粘貼到一個新的控制檯應用程序的代碼。我注意到這是在2006年寫的,可能是因爲我使用的是Windows 7? – samwa 2011-01-23 20:36:42

4

,對我的X按鈕
祕密適用於Windows 7關閉是靜態變量ConsoleEventDelegate _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 
    } 
相關問題