AppDomain.CurrentDomain.ProcessExit和清理 [英] AppDomain.CurrentDomain.ProcessExit and cleanup
问题描述
我有简单的应用程序,当我需要使用停止停止底色()的线程应用程序之前的功能已关闭。问题是,我的main()函数有几个出口点(return语句)
I have simple application when I need to stop a backround thread using Stop() function before application is closed. The problem is that my Main() function has several exit points (return statements)
static void Main(string[] args)
{
/// some code
return;
// some code
return;
//// etc
}
我试图用AppDomain.CurrentDomain.ProcessExit作为清理一个劲儿地地方,但它永远不会被调用(至少同时有一个后台线程)。有没有办法制定 一些不错的方式来实现呢?
I tried to use AppDomain.CurrentDomain.ProcessExit as a signle place for clean up but it is never called (at least while there is a background thread). Is there a way to work out some nice way to implement that?
推荐答案
你可以用你code在一个单独的方法,并从主()调用它:
You can wrap all you code in a separate method and call it from Main():
static void Main(string[] args)
{
DoSomething();
TerminateThread(); // Thread.Stop() code goes here
}
static void DoSomething()
{
/// some code
return;
// some code
return;
//// etc
}
这篇关于AppDomain.CurrentDomain.ProcessExit和清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!