c# - AppDomain.CurrentDomain.Process退出和清理

标签 c# .net events

当我需要在关闭应用程序之前使用 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/

相关文章:

c# - 如何对 Observable.Sample() 进行单元测试?

c# - APP.xaml 中带有事件的 XAML 外包数据模板

.net - 为什么这些WCF相关资源没有被清理掉?

c# - 如何将数据从一个数据库导入到另一个数据库

java - 使用全局事件总线有哪些注意事项?

c# - 如何为数组中的对象创建事件?

javascript - 简单 CoffeeScript 和 jQuery 事件的奇怪问题

c# - Visual Studio 2012 添加对错误 DLL 的引用

c# - 在WPF中,如何调试触发器?

c# - 如何减小结构尺寸