static void Main(string[] args) { for (var i = 0; i < args.Length; i++) { if (args[i] == "--debug") { IsDebug = true; } if (args[i] == "--default") { ForceDefaultMapping = true; } if (args[i] == "--skip-ui") { ShouldOpenUI = false; } if (args[i] == "--mapping") { InitialMappingName = args[i + 1]; i++; } if (args[i] == "--start-disabled") { ShouldStartDisabled = true; } } RestServerInstance = new RestServer(); KeyboardMapperInstance = new KeyboardMapper(); ControllerManagerInstance = new ControllerManager(); KeyboardMapperInstance.OnParse += (s, e) => { if (ControllerManagerInstance.IsRunning) { ControllerManagerInstance.Stop(); ControllerManagerInstance.Start(); } }; RestServerInstance.Start(); if (!ShouldStartDisabled) { KeyboardMapperInstance.Start(); ControllerManagerInstance.Start(); } // See https://github.com/gmamaladze/globalmousekeyhook/issues/3#issuecomment-230909645 System.Windows.Forms.ApplicationContext msgLoop = new System.Windows.Forms.ApplicationContext(); ConsoleCtrlHandlerRef += new ConsoleCtrlHandler(HandleConsoleExit); SetConsoleCtrlHandler(ConsoleCtrlHandlerRef, true); System.Windows.Forms.Application.Run(msgLoop); }
/// <summary> /// Adds or removes a Console Control Handler /// </summary> /// <param name="handlerRoutine">handler to be added or removed</param> /// <param name="add">true = add handler, false = remove handler</param> public static void SetConsoleCtrlHandler(ConsoleCtrlHandler handlerRoutine, bool add) { Arguments.NotNull(handlerRoutine, nameof(handlerRoutine)); if (!NativeMethods.SetConsoleCtrlHandler(handlerRoutine, add)) { throw new Win32Exception(Marshal.GetLastWin32Error(), "unable to Set Console Ctrl Handler"); } }
/// <summary> /// Adds an action that will be executed when the Application exists. /// </summary> /// <param name="action"></param> public static void AddApplicationExitHandler(Action action) { EventHandler eventHandler = (sender, evt) => action(); processHooks.Add(eventHandler); AppDomain.CurrentDomain.ProcessExit += eventHandler; ConsoleCtrlHandler consoleCtrlHandler = type => { action(); return(false); }; ctrlHandlers.Add(consoleCtrlHandler); SetConsoleCtrlHandler(consoleCtrlHandler, true); }
//--------------------------------------------------------------------- public EsEngineConsoleModify(string app_title) { mAppTitle = app_title; Console.ForegroundColor = ConsoleColor.Green; // 禁用“关闭”按钮 IntPtr CLOSE_MENU = GetSystemMenu(Process.GetCurrentProcess().MainWindowHandle, IntPtr.Zero); int SC_CLOSE = 0xF060; RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0); mConsoleCtrlHandler = new ConsoleCtrlHandler(_consoleCtrlHandler); SetConsoleCtrlHandler(mConsoleCtrlHandler, true); if (mThreadMonitorExit == null) { mThreadMonitorExit = new Thread(new ThreadStart(_threadMonitorExit)); mThreadMonitorExit.Name = "MonitorExit"; mThreadMonitorExit.IsBackground = true; mThreadMonitorExit.Start(); } }
//--------------------------------------------------------------------- public EsEngineConsoleModify(string app_title) { mAppTitle = app_title; Console.ForegroundColor = ConsoleColor.Green; // 禁用“关闭”按钮 IntPtr CLOSE_MENU = GetSystemMenu(Process.GetCurrentProcess().MainWindowHandle, IntPtr.Zero); int SC_CLOSE = 0xF060; RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0); mConsoleCtrlHandler = new ConsoleCtrlHandler(_consoleCtrlHandler); SetConsoleCtrlHandler(mConsoleCtrlHandler, true); if (mThreadMonitorExit == null) { mThreadMonitorExit = new Thread(new ThreadStart(_threadMonitorExit)); mThreadMonitorExit.Name = "MonitorExit"; mThreadMonitorExit.IsBackground = true; mThreadMonitorExit.Start(); } }
public void Run() { if (!Environment.UserInteractive) { Run(this); } else { WarnIfAlreadyRunningAsAService(); var handle = new ManualResetEvent(false); _consoleCtrlHandler += new ConsoleCtrlHandler(c => { this.WriteToConsole(ConsoleColor.Cyan, $"Stop requested, stopping {this.Name}..."); this.StopInteractive(); handle.Set(); return(true); }); SetConsoleCtrlHandler(_consoleCtrlHandler, true); this.StartInteractive(handle); handle.WaitOne(); } }
static void Main(string[] args) { try { using (var service = new ElasticsearchService(args)) { if (Environment.UserInteractive) { WarnRunningService(); var handle = new ManualResetEvent(false); _consoleCtrlHandler += new ConsoleCtrlHandler(c => { ElasticsearchConsole.WriteLine(ConsoleColor.Red, "Stopping Elasticsearch..."); service.StopInteractive(); handle.Set(); return(false); }); SetConsoleCtrlHandler(_consoleCtrlHandler, true); service.StartInteractive(); handle.WaitOne(); } else { ServiceBase.Run(service); } } } catch (Exception e) { e.ToEventLog("Elasticsearch"); if (Environment.UserInteractive) { e.ToConsole("An exception occurred in Main()"); } } }
public static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
public static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler consoleCtrlHandler, bool Add);
static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler ctrlHandler, bool add);
static Console() { Handler = ConsoleCtrlHandler; ConsoleApi.SetConsoleCtrlHandler(Handler, true); SystemEvents.SessionEnding += SystemEvents_SessionEnding; }
internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handlerRoutine, [MarshalAs(UnmanagedType.Bool)] bool add);