예제 #1
0
        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);
        }
예제 #2
0
        /// <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");
            }
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        //---------------------------------------------------------------------
        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();
            }
        }
예제 #5
0
        //---------------------------------------------------------------------
        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();
            }
        }
예제 #6
0
        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();
            }
        }
예제 #7
0
        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()");
                }
            }
        }
예제 #8
0
 public static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
예제 #9
0
 private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
예제 #10
0
 private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handler, bool add);
예제 #11
0
 public static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler consoleCtrlHandler, bool Add);
예제 #12
0
 static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler ctrlHandler, bool add);
예제 #13
0
 static Console()
 {
     Handler = ConsoleCtrlHandler;
     ConsoleApi.SetConsoleCtrlHandler(Handler, true);
     SystemEvents.SessionEnding += SystemEvents_SessionEnding;
 }
예제 #14
0
 internal static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandler handlerRoutine, [MarshalAs(UnmanagedType.Bool)] bool add);