据我所知,没有直截了当的答案。
如果你想写一个健壮的 DLL,你应该准备几个场景:
- 您的代码托管在默认 AppDomain 中的 .NET 应用程序中。 (琐碎的场景)
- 您的代码托管在 .NET 应用程序中,位于由宿主代码创建的 AppDomain 中。
- 您的代码托管在非托管应用程序(托管 CLR)中。
第 3 种情况最难处理,因为 CLR 可以被其宿主禁用,因此托管代码将不再执行。
System.Windows.Forms.Application.ApplicationExit 不好,因为它只适用于 WinForm 应用程序。
System.AppDomain.DomainUnload 本身并不好,因为它永远不会为默认的 AppDomain 引发。
AppDomain.ProcessExit 本身并不好:如果您的代码托管在单独的 AppDomain 中,则主机可能会卸载该 AppDomain,因此永远不会引发事件。
我会首先尝试涵盖大多数情况,使用以下内容:
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
AppDomain.CurrentDomain.ProcessExit += MyTerminationHandler;
else
AppDomain.CurrentDomain.DomainUnload += MyTerminationHandler;
但请注意以下备注 (from MSDN):
所有 ProcessExit 事件处理程序的总执行时间是有限的,就像所有终结器的总执行时间在进程关闭时受到限制一样。默认值为两秒。非托管主机可以通过使用 OPR_ProcessExit 枚举值调用 ICLRPolicyManager::SetTimeout 方法来更改此执行时间。
上面的代码仍然无人看管第 3 个场景。
我知道有两种方法可以处理这种情况(以及前两种)
首先可以使用System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup方法,如下:
{
// this goes at your code's entry point
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(MyExecutionCode, MyCleanupCode, null);
}
static void MyExecutionCode(object data) { /* your execution code here */}
static void MyCleanupCode(object data, bool exceptionThrown) { /* your cleanup code here */ }
其次,您可以通过继承 System.Runtime.ConstrainedExecution.CriticalFinalizerObject 类 (see MSDN here) 并将清理代码放入终结器中来使用它。这要求您的清理代码遵守Constrained Execution Region 准则。