2011-01-23 3 views
5

Windowsのc#のコンソールでctrl + cイベントに対処する方法を知っている人はいますか?Windows 7でコンソールを終了するC#をキャプチャ

この質問には:Capture console exit C#が表示されていますが、試してみましたが、ユーザーがコンソールウィンドウの上部にあるXを閉じるとイベントがキャプチャされます。

ユーザがctrl + cをタイプしても何も起こりません。デバッグ時にハンドラにヒットしません。

おかげ

は、ここに私のコード

あなたはハンドラにConsole.CancelKeyPressイベントを配線する必要がある
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

を_D変数、静的ですCtrl + Cを探します。これをテストするにはCtrl + F5でプログラムを起動してください。 –

答えて

1

です。 Hereは、このトピックに関する素晴らしい記事です。 X-ボタンでWindows 7の閉会
秘密に私の作品

+0

残念ながらそれは私のためには機能しません、私はコピーし、直接新しいコンソールアプリケーションにコードを貼り付けました。私はこれが2006年に書かれたことに気付きました。なぜなら、Windows 7を使用しているからでしょうか? – samwa

+0

http://msdn.microsoft.com/en-us/library/system.console.cancelkeypress.aspx – awright18

+0

あなたの質問にあなたが試みたコードを追加することができます正しいはずです。 Console.CancelKeyPress + =新しいConsoleCancelEventHandler(myHandler)を追加する必要があります。あなたのメインの方法に – awright18

4

は、ConsoleEventDelegateはデバッガが邪魔になる

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 
    } 
関連する問題