private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
        {
            switch (ctrlType)
            {
            case CtrlTypes.CTRL_C_EVENT:
                Console.WriteLine("CTRL+C received!");
                break;

            case CtrlTypes.CTRL_BREAK_EVENT:
                Console.WriteLine("CTRL+BREAK received!");
                break;

            case CtrlTypes.CTRL_CLOSE_EVENT:
                Console.WriteLine("Program being closed!");
                break;

            case CtrlTypes.CTRL_LOGOFF_EVENT:
            case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                Console.WriteLine("User is logging off!");
                break;
            }

            onExitAction();
            ConsoleImports.FreeConsole();
            return(true);
        }
        public void OpenConsole(Action <bool> switchOn)
        {
            var opened = ConsoleImports.AllocConsole();

            while (!opened)
            {
                ConsoleImports.FreeConsole();
                opened = ConsoleImports.AllocConsole();
            }
            IntPtr hwnd    = ConsoleImports.GetConsoleWindow();
            IntPtr hmenu   = ConsoleImports.GetSystemMenu(hwnd, false);
            uint   hWindow = ConsoleImports.EnableMenuItem(hmenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

            ConsoleImports.DeleteMenu(hmenu, SC_CLOSE, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);

            switchOn(false);
        }
 public void Free()
 {
     busy = false;
     var res = ConsoleImports.SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
 }
 public void Busy()
 {
     busy = true;
     var res = ConsoleImports.SetConsoleCtrlHandler(null, false);
 }
 public void RemoveHandler()
 {
     var res = ConsoleImports.SetConsoleCtrlHandler(null, false);
 }
        public void SetHandler()
        {
            var res = ConsoleImports.SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);

            Console.CancelKeyPress += HandleConsole_CancelKeyPress;
        }