/// <summary>
        ///     callback for Kernel32's SetConsoleCtrlHandler
        /// </summary>
        /// <param name="ctrlType">the control event received</param>
        /// <returns>true if the callback handled the event</returns>
        private bool OnConsoleCtrlEvent(ConsoleCtrlType ctrlType)
        {
            switch (ctrlType)
            {
            case ConsoleCtrlType.CtrlCEvent:
            case ConsoleCtrlType.CtrlBreakEvent:
            case ConsoleCtrlType.CloseEvent:
            case ConsoleCtrlType.LogOffEvent:
            case ConsoleCtrlType.ShutdownEvent:
            {
                this.ShutdownSignal.TrySetResult(true);
                return(false);
            }

            default:
            {
                throw AbbotwareException.Create("Unexpected Shutdown Event Recieved: {0} Should not reach this code", ctrlType);
            }
            }
        }
Exemple #2
0
            private static void ExitHandler(Object sender, FormClosingEventArgs e)
            {
                if (e.Cancel || e.CloseReason == CloseReason.None)
                {
                    return;
                }

                ConsoleCtrlType type = e.CloseReason switch
                {
                    CloseReason.WindowsShutDown => ConsoleCtrlType.CtrlShutdownEvent,
                    CloseReason.MdiFormClosing => ConsoleCtrlType.CtrlShutdownEvent,
                    CloseReason.UserClosing => ConsoleCtrlType.CtrlCloseEvent,
                    CloseReason.TaskManagerClosing => ConsoleCtrlType.CtrlTaskManagerClosing,
                    CloseReason.FormOwnerClosing => ConsoleCtrlType.CtrlShutdownEvent,
                    CloseReason.ApplicationExitCall => ConsoleCtrlType.CtrlCloseEvent,
                    CloseReason.None => throw new NotSupportedException(),
                          _ => throw new NotSupportedException()
                };

                e.Cancel = ExitHandler(type);
            }
Exemple #3
0
 private static Boolean ExitHandler(ConsoleCtrlType type)
 {
     return(ExitHandle && OnConsoleExit(type));
 }