AppDomain.CurrentDomain.ProcessExit和cleanup
当我需要在应用程序关闭之前使用Stop()函数停止后台线程时,我有简单的应用程序。 问题是我的Main()函数有几个退出点(return语句)
static void Main(string[] args) { /// some code return; // some code return; //// etc }
我尝试使用AppDomain.CurrentDomain.ProcessExit作为清理的标志位置,但它永远不会被调用(至少在有后台线程时)。 有没有办法找出一些很好的方法来实现它?
您可以将所有代码包装在一个单独的方法中,并从Main()中调用它:
static void Main(string[] args) { DoSomething(); TerminateThread(); // Thread.Stop() code goes here } static void DoSomething() { /// some code return; // some code return; //// etc }
改变return;
调用并调用也终止该进程的清理例程。
您可以使用Application.ApplicationExit Event
根据MSDN的事件:
应用程序即将关闭时发生。