AppDomain.CurrentDomain.ProcessExit和cleanup

Cap*_*mic 5 .net c# events

当我需要在关闭应用程序之前使用Stop()函数停止后台线程时,我有简单的应用程序.问题是我的Main()函数有几个退出点(return语句)

static void Main(string[] args)
{
/// some code
return;

// some code
return;

//// etc
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用AppDomain.CurrentDomain.ProcessExit作为清理的标志位置,但它永远不会被调用(至少在有后台线程时).有没有办法找出一些很好的方法来实现它?

Igo*_*hov 5

您可以将所有代码包装在一个单独的方法中,并从Main()中调用它:

static void Main(string[] args)
{
  DoSomething();
  TerminateThread(); // Thread.Stop() code goes here
}

static void DoSomething()
{
   /// some code
   return;

   // some code
   return;

   //// etc
}
Run Code Online (Sandbox Code Playgroud)