private void Dispose()
 {
     if (this.windowHandle != IntPtr.Zero)
     {
         if (registeredSessionNotification)
         {
             Microsoft.Win32.UnsafeNativeMethods.WTSUnRegisterSessionNotification(new HandleRef(systemEvents, systemEvents.windowHandle));
         }
         IntPtr windowHandle = this.windowHandle;
         this.windowHandle = IntPtr.Zero;
         HandleRef hWnd = new HandleRef(this, windowHandle);
         if (Microsoft.Win32.UnsafeNativeMethods.IsWindow(hWnd) && (this.DefWndProc != IntPtr.Zero))
         {
             Microsoft.Win32.UnsafeNativeMethods.SetWindowLong(hWnd, -4, new HandleRef(this, this.DefWndProc));
             Microsoft.Win32.UnsafeNativeMethods.SetClassLong(hWnd, -24, this.DefWndProc);
         }
         if (Microsoft.Win32.UnsafeNativeMethods.IsWindow(hWnd) && !Microsoft.Win32.UnsafeNativeMethods.DestroyWindow(hWnd))
         {
             Microsoft.Win32.UnsafeNativeMethods.PostMessage(hWnd, 0x10, IntPtr.Zero, IntPtr.Zero);
         }
         else
         {
             IntPtr moduleHandle = Microsoft.Win32.UnsafeNativeMethods.GetModuleHandle(null);
             Microsoft.Win32.UnsafeNativeMethods.UnregisterClass(className, new HandleRef(this, moduleHandle));
         }
     }
     if (this.consoleHandler != null)
     {
         Microsoft.Win32.UnsafeNativeMethods.SetConsoleCtrlHandler(this.consoleHandler, 0);
         this.consoleHandler = null;
     }
 }
 private void Initialize()
 {
     this.consoleHandler = new Microsoft.Win32.NativeMethods.ConHndlr(this.ConsoleHandlerProc);
     if (!Microsoft.Win32.UnsafeNativeMethods.SetConsoleCtrlHandler(this.consoleHandler, 1))
     {
         this.consoleHandler = null;
     }
     this.windowHandle = this.CreateBroadcastWindow();
     AppDomain.CurrentDomain.ProcessExit += new EventHandler(SystemEvents.Shutdown);
     AppDomain.CurrentDomain.DomainUnload += new EventHandler(SystemEvents.Shutdown);
 }
 public static extern bool SetConsoleCtrlHandler(Microsoft.Win32.NativeMethods.ConHndlr handler, int add);