【问题标题】:Get close window message in Hidden C# Console Application在隐藏的 C# 控制台应用程序中获取关闭窗口消息
【发布时间】:2011-02-13 20:21:03
【问题描述】:

我有一个 Windows 窗体,它在后台启动一些控制台应用程序 (CreateNoWindow = rue,WindowStyle = ProcessWindowStyle.Hidden)。

Windows 窗体让我有机会随时停止控制台应用程序。但我想以某种方式处理控制台应用程序中的关闭消息。我尝试使用如下钩子:

    [DllImport("Kernel32")]
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add);

    // A delegate type to be used as the handler routine 
    // for SetConsoleCtrlHandler.
    public delegate bool HandlerRoutine(CtrlTypes ctrlType);

    // An enumerated type for the control messages
    // sent to the handler routine.
    public enum CtrlTypes
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT,
        CTRL_CLOSE_EVENT,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
    {
        StaticLogger.Instance.DebugFormat("Main: ConsoleCtrlCheck: Got event {0}.", ctrlType);
        if (ctrlType == CtrlTypes.CTRL_CLOSE_EVENT)
        {
            // Handle close stuff
        }
        return true;
    }

    static int Main(string[] args)
    {
        // Subscribing
        HandlerRoutine hr = new HandlerRoutine(ConsoleCtrlCheck);
        SetConsoleCtrlHandler(hr, true);
        // Doing stuff
    }

但只有在创建控制台窗口时,我才会在 ConsoleCtrlCheck 中收到消息。但如果窗口被隐藏 - 我没有收到任何消息。

在我的 Windows 窗体中关闭我使用的控制台应用程序进程 proc.CloseMainWindow(); 将消息发送到控制台窗口。

附: AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; - 也无济于事

您现在有其他方法来处理这种情况吗? 谢谢。

【问题讨论】:

    标签: c# console message hidden


    【解决方案1】:

    这可能有效。我在 NUnit testes 中使用它来清理环境。不幸的是,它不能被调用。要使其正常工作,您需要创建它的一个实例并传递应在关机时调用的回调函数。

        /// <summary>
        /// Detects the moment when environment is about to be shutdown.
        /// <remarks>
        ///     For usage just create single instance of it.
        ///     Each time when GC calles Finilize a '~ShutdownDetector' will be called.
        /// </remarks>
        /// </summary>
        public sealed class ShutdownDetector
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="T:ShutdownDetector"/> class.
            /// </summary>
            /// <param name="notifier">The notifier</param>
            public ShutdownDetector(Notifier notifier)
            {
                if (notifier == null) throw new ArgumentNullException("notifier");
                _notifier = notifier;
            }
    
            /// <summary>
            /// Releases unmanaged resources and performs other cleanup operations before the
            /// <see cref="T:CQG.PDTools.Common.ShutdownDetector"/> is reclaimed by garbage collection.
            /// </summary>
            ~ShutdownDetector()
            {
                if (Environment.HasShutdownStarted)
                {
                    onShutdown();
                }
                else
                {
                    new ShutdownDetector(_notifier);
                }
            }
    
            /// <summary>
            /// Called when component needs to signal about shutdown.
            /// </summary>
            private void onShutdown()
            {
                if (_notifier != null)
                {
                    _notifier();
                }
            }
    
            Notifier _notifier;
            public delegate void Notifier();
        }
    

    【讨论】:

    • 有趣...不幸的是,已经过去了很多时间,但我会试一试并报告结果。谢谢!为什么它不能保证有效?
    • 在应用关闭框架上为 finalyzers 提供了一定的时间(~2 秒)。 Finalyzers 需要更多的框架只是终止进程。
    猜你喜欢
    • 2011-04-20
    • 2011-04-04
    • 1970-01-01
    • 2016-03-30
    • 2012-10-14
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多