示例#1
0
        private async Task <bool> SendCtrlSignal(ConsoleCtrlEvent signal, TimeSpan timeout)
        {
            Trace.Info($"Sending {signal} to process {_proc.Id}.");
            ConsoleCtrlDelegate ctrlEventHandler = new ConsoleCtrlDelegate(ConsoleCtrlHandler);

            try
            {
                if (!FreeConsole())
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                if (!AttachConsole(_proc.Id))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                if (!SetConsoleCtrlHandler(ctrlEventHandler, true))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                if (!GenerateConsoleCtrlEvent(signal, 0))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                Trace.Info($"Successfully send {signal} to process {_proc.Id}.");
                Trace.Info($"Waiting for process exit or {timeout.TotalSeconds} seconds after {signal} signal fired.");
                var completedTask = await Task.WhenAny(Task.Delay(timeout), _processExitedCompletionSource.Task);

                if (completedTask == _processExitedCompletionSource.Task)
                {
                    Trace.Info("Process exit successfully.");
                    return(true);
                }
                else
                {
                    Trace.Info($"Process did not honor {signal} signal within {timeout.TotalSeconds} seconds.");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Trace.Info($"{signal} signal doesn't fire successfully.");
                Trace.Verbose($"Catch exception during send {signal} event to process {_proc.Id}");
                Trace.Verbose(ex.ToString());
                return(false);
            }
            finally
            {
                FreeConsole();
                SetConsoleCtrlHandler(ctrlEventHandler, false);
            }
        }
示例#2
0
        private bool ConsoleCtrlHandler(ConsoleCtrlEvent ctrlType)
        {
            switch (ctrlType)
            {
            case ConsoleCtrlEvent.CTRL_C:
                _trace?.Info($"Ignore Ctrl+C to current process.");
                return(true);

            case ConsoleCtrlEvent.CTRL_BREAK:

                _trace?.Info($"Ignore Ctrl+Break to current process.");
                return(true);
            }

            return(false);
        }
示例#3
0
        private bool ConsoleCtrlHandler(ConsoleCtrlEvent ctrlType)
        {
            switch (ctrlType)
            {
            case ConsoleCtrlEvent.CTRL_C:
                Trace.Info($"Ignore Ctrl+C to current process.");
                // We return True, so the default Ctrl handler will not take action.
                return(true);

            case ConsoleCtrlEvent.CTRL_BREAK:
                Trace.Info($"Ignore Ctrl+Break to current process.");
                // We return True, so the default Ctrl handler will not take action.
                return(true);
            }

            // If the function handles the control signal, it should return TRUE.
            // If it returns FALSE, the next handler function in the list of handlers for this process is used.
            return(false);
        }
示例#4
0
 private static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId);
示例#5
0
 public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId);
示例#6
0
 public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent dwCtrlEvent, uint dwProcessGroupId);
示例#7
0
 public static void SendSignal(this AccessCommandPrompt prompt, ConsoleCtrlEvent signal)
 {
     GenerateConsoleCtrlEvent(signal, ProcessExtensions.GetProcessByHandle(prompt.Handle).Id);
 }
示例#8
0
 public static extern bool GenerateConsoleCtrlEvent(this int dwProcessGroupId, ConsoleCtrlEvent sigevent);
 public static extern bool GenerateConsoleCtrlEvent(
     [In] ConsoleCtrlEvent sigevent,
     [In] uint processGroupId);