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

ldxq2e6h  于 2022-12-01  发布在  .NET
关注(0)|答案(5)|浏览(421)

在.NET中,是否有一种方法(如事件)用于检测控制台应用程序何时退出?我需要清理一些线程和COM对象。
我正在从控制台应用程序运行一个消息循环,没有表单。我正在使用的一个DCOM组件似乎要求应用程序泵送消息。
我已尝试将行程常式加入至Process.GetCurrentProcess.Exited和Process.GetCurrentProcess.Disposed。
我也尝试过给 * Application.ApplicationExit * 和 * Application.ThreadExit * 事件添加一个处理程序,但是它们没有触发。也许这是因为我没有使用窗体。

jucafojl

jucafojl1#

您可以使用AppDomainProcessExit事件:

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);           
        // do some work

    }

    static void CurrentDomain_ProcessExit(object sender, EventArgs e)
    {
        Console.WriteLine("exit");
    }
}

更新

下面是一个完整的示例程序,其中有一个空的“消息泵”运行在一个单独的线程上,它允许用户在控制台中输入一个退出命令,以优雅地关闭应用程序。在MessagePump中的循环之后,您可能希望以一种良好的方式清理线程使用的资源。在MessagePump中这样做比在ProcessExit中更好,原因如下:

  • 避免跨线程问题;如果外部COM对象是在MessagePump线程上创建的,那么在那里处理它们会更容易。
  • ProcessExit有一个时间限制(默认为3秒),因此如果清理非常耗时,则在该事件处理程序中执行清理可能会失败。

代码如下:

class Program
{
    private static bool _quitRequested = false;
    private static object _syncLock = new object();
    private static AutoResetEvent _waitHandle = new AutoResetEvent(false);

    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        // start the message pumping thread
        Thread msgThread = new Thread(MessagePump);
        msgThread.Start();
        // read input to detect "quit" command
        string command = string.Empty;
        do
        {
            command = Console.ReadLine();
        } while (!command.Equals("quit", StringComparison.InvariantCultureIgnoreCase));
        // signal that we want to quit
        SetQuitRequested();
        // wait until the message pump says it's done
        _waitHandle.WaitOne();
        // perform any additional cleanup, logging or whatever
    }

    private static void SetQuitRequested()
    {
        lock (_syncLock)
        {
            _quitRequested = true;
        }
    }

    private static void MessagePump()
    {
        do
        {
            // act on messages
        } while (!_quitRequested);
        _waitHandle.Set();
    }

    static void CurrentDomain_ProcessExit(object sender, EventArgs e)
    {
        Console.WriteLine("exit");
    }
}
nhaq1z21

nhaq1z212#

下面是一个完整的、非常简单的.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..");
        }
    }
 }
osh3o9ms

osh3o9ms3#

应用程序是一个服务器,它只运行到系统关闭或收到Ctrl + C或控制台窗口关闭为止。
由于应用程序的特殊性质,“优雅地”退出是不可行的。(我可能会编写另一个应用程序,它将发送“服务器关闭”消息,但这对于一个应用程序来说是多余的,而且对于某些情况(例如服务器(实际上是操作系统)实际上正在关闭)来说仍然是不够的。)
由于这些情况,我添加了一个“ConsoleCtrlHandler“,在那里我停止我的线程并清理我的COM对象,等等...

Public Declare Auto Function SetConsoleCtrlHandler Lib "kernel32.dll" (ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As Boolean

Public Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As Boolean

Public Enum CtrlTypes
  CTRL_C_EVENT = 0
  CTRL_BREAK_EVENT
  CTRL_CLOSE_EVENT
  CTRL_LOGOFF_EVENT = 5
  CTRL_SHUTDOWN_EVENT
End Enum

Public Function ControlHandler(ByVal ctrlType As CtrlTypes) As Boolean
.
.clean up code here
.
End Function

Public Sub Main()
.
.
.
SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ControlHandler), True)
.
.
End Sub

这个设置看起来很完美。下面是a link到一些C#代码的相同内容。

mrfwxfqh

mrfwxfqh4#

对于CTRL+C的情况,您可以使用以下代码:

// Tell the system console to handle CTRL+C by calling our method that
// gracefully shuts down.
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
            Console.WriteLine("Shutting down...");
            // Cleanup here
            System.Threading.Thread.Sleep(750);
}
2izufjch

2izufjch5#

如果您使用的是控制台应用程序并且正在发送消息,则可以使用WM_QUIT消息。

相关问题