当我需要在关闭应用程序之前使用 Stop() 函数停止后台线程时,我有一个简单的应用程序。问题是我的 Main() 函数有几个退出点(返回语句)
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
}
关于c# - AppDomain.CurrentDomain.Process退出和清理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2093880/