Here's some sample code:
using System;
namespace UnloadFromFinalizer
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
}
AppDomain domain;
Program()
{
this.domain = AppDomain.CreateDomain("MyDomain");
}
~Program()
{
AppDomain.Unload(this.domain);//<-- Exception thrown here
}
}
}
I have a class that creates an AppDomain in the constructor to be used over the lifetime of the object. I'd like to properly cleanup the AppDomain, so I thought I would call Unload in the finalizer. Unfortunately, that causes a CannotUnloadAppDomainException to be thrown. The MSDN documentation for AppDomain.Unload notes:
In some cases, calling Unload causes an immediate CannotUnloadAppDomainException, for ample if it is called in a finalizer.
Why is this? Is the member variable "domain" already cleaned up? Does that cleanup automatically include unloading the AppDomain, or will it still exist in some unreachable way? Is there something I should be doing, or can I safely just dump the finalizer? (I don't really care when the GC gets rid of my object so long as it's fully cleaned up in the process.)
The AppDomain
class does not have a finalizer defined and so will just be garbage collected as normal. The finalizer of your Program
class will be called from the finalizer thread of the garbage collector. When this happens, there is no guarantee that your AppDomain
instance will or will not have been garbage collected yet and so you will get undetermined behaviour.
I would not bother with the finalizer of Program
, as the AppDomain
will get garbage collected anyway, plus in addition, the whole process will be destroyed when the Main
method exits anyway.
来源:https://stackoverflow.com/questions/1891480/why-does-appdomain-unload-error-in-finalizer