为了账号安全,请及时绑定邮箱和手机立即绑定

.NET控制台应用程序退出事件

.NET控制台应用程序退出事件

手掌心 2019-08-27 16:35:42
.NET控制台应用程序退出事件在.NET中,是否有一种方法(如事件)用于检测控制台应用程序何时退出?我需要清理一些线程和COM对象。我正在从控制台应用程序运行一个没有表单的消息循环。我正在使用的DCOM组件似乎要求应用程序泵消息。我已经尝试向Process.GetCurrentProcess.Exited和Process.GetCurrentProcess.Disposed添加一个处理程序。我还尝试向Application.ApplicationExit和Application.ThreadExit事件添加处理程序,但它们没有触发。也许那是因为我没有使用表格。
查看完整描述

3 回答

?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

这是一个完整的,非常简单的.Net解决方案,适用于所有版本的Windows。只需将其粘贴到一个新项目中,运行它并尝试CTRL-C来查看它如何处理它:

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices;using System.Text;using System.Threading;namespace TestTrapCtrlC{
    public class Program{
        static bool exitSystem = false;

        #region Trap application termination
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

        private delegate bool EventHandler(CtrlType sig);
        static EventHandler _handler;

        enum CtrlType {
         CTRL_C_EVENT = 0,
         CTRL_BREAK_EVENT = 1,
         CTRL_CLOSE_EVENT = 2,
         CTRL_LOGOFF_EVENT = 5,
         CTRL_SHUTDOWN_EVENT = 6
         }

        private static bool Handler(CtrlType sig) {
            Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");

            //do your cleanup here
            Thread.Sleep(5000); //simulate some cleanup delay

            Console.WriteLine("Cleanup complete");

            //allow main to run off
             exitSystem = true;

            //shutdown right away so there are no lingering threads
            Environment.Exit(-1);

            return true;
        }
        #endregion

        static void Main(string[] args) {
            // Some biolerplate to react to close window event, CTRL-C, kill, etc
            _handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(_handler, true);

            //start your multi threaded program here
            Program p = new Program();
            p.Start();

            //hold the console so it doesn’t run off the end
            while(!exitSystem) {
                Thread.Sleep(500);
            }
        }

        public void Start() {
            // start a thread and start doing some processing
            Console.WriteLine("Thread started, processing..");
        }
    }
 }


查看完整回答
反对 回复 2019-08-27
?
慕少森

TA贡献2019条经验 获得超9个赞

该应用程序是一个服务器,它只运行直到系统关闭或接收到Ctrl + C或控制台窗口关闭。

由于应用程序的特殊性,“优雅地”退出是不可行的。(可能是我可以编写另一个会发送“服务器关闭”消息的应用程序,但这对于一个应用程序来说是过度的,而且在某些情况下仍然不够,例如当服务器(实际操作系统)实际关闭时。)

由于这些情况,我添加了一个“ ConsoleCtrlHandler ”,我停止我的线程并清理我的COM对象等...

Public Declare Auto Function SetConsoleCtrlHandler Lib "kernel32.dll" (ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As BooleanPublic Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As BooleanPublic Enum CtrlTypes
  CTRL_C_EVENT = 0
  CTRL_BREAK_EVENT
  CTRL_CLOSE_EVENT
  CTRL_LOGOFF_EVENT = 5
  CTRL_SHUTDOWN_EVENTEnd EnumPublic Function ControlHandler(ByVal ctrlType As CtrlTypes) As Boolean..clean up code here.End FunctionPublic Sub Main()...SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ControlHandler), True)..End Sub

这种设置似乎完美无缺。这是一个链接到同一件事的一些C#代码。


查看完整回答
反对 回复 2019-08-27
  • 3 回答
  • 0 关注
  • 1877 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信