.NET控制台应用程序退出事件 [英] .NET Console Application Exit Event

查看:273
本文介绍了.NET控制台应用程序退出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中,有没有一种方法,比如一个事件,因为当一个控制台应用程序退出检测?我需要清理一些线程和COM对象。

In .NET, is there a method, such as an event, for detecting when a Console Application is exiting? I need to clean up some threads and COM objects.

我正在运行的消息循环,没有一种形式,从控制台应用程序。我正在使用DCOM组件似乎需要应用程序泵的消息。

I am running a message loop, without a form, from the console application. A DCOM component that I am using seems to require that the application pump messages.

我曾尝试添加一个处理程序Process.GetCurrentProcess.Exited和Process.GetCurrentProcess.Disposed。

I have tried adding a handler to Process.GetCurrentProcess.Exited and Process.GetCurrentProcess.Disposed.

我也尝试添加一个处理程序Application.ApplicationExit和Application.ThreadExit事件,但他们没有开火。也许这是因为我没有使用的一种形式。

I have also tried adding a handler to Application.ApplicationExit and Application.ThreadExit events, but they are not firing. Perhaps that is because I am not using a form.

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.appdomain.processexit.aspx"><$c$c>ProcessExit事件 的AppDomain 的:

You can use the ProcessExit event of the AppDomain:

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");
    }
}

更新

下面是一个空的消息泵在一个单独的线程上运行,允许用户输入在控制台quit命令正常关闭应用程序的完整示例程序。在MessagePump循环之后,你可能会想清理一个很好的方式使用的线程资源。这是更好地做到这一点比ProcessExit有以下几个原因:

Here is a full example program with an empty "message pump" running on a separate thread, that allows the user to input a quit command in the console to close down the application gracefully. After the loop in MessagePump you will probably want to clean up resources used by the thread in a nice manner. It's better to do that there than in ProcessExit for several reasons:

  • 避免交叉线程的问题;如果MessagePump线程上创建外部COM对象,它更容易对付他们那里。
  • 上有ProcessExit一个时限(3秒),所以如果清理是耗时的,如果pefromed该事件处理程序中,可能会失败。

下面是code:

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");
    }
}

这篇关于.NET控制台应用程序退出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆