我试图卸载应用程序域,但我的应用程序停止工作

I tried to unload an Application domain but my application stopped working

我创建了一个新的应用程序域并将程序集加载到其中。

Assembly assembly = dom.Load("bookOne");

一切都很顺利。该应用程序使用了资源字典等,但是当我尝试使用此代码卸载 dll 以释放内存时,我的应用程序关闭而没有任何错误或警告:

try
{
    AppDomain.Unload(dom);
}
catch (CannotUnloadAppDomainException)
{
    Console.WriteLine("Book Unloaded!" + " 4");
    AppDomain.Unload(dom);
    GC.Collect();
}

突然退出后输出window显示这些:

A first chance exception of type 'System.AppDomainUnloadedException' occurred in mscorlib.dll A first chance exception of type 'System.AppDomainUnloadedException' occurred in mscorlib.dll The thread 'vshost.RunParkingWindow' (0x1df8) has exited with code 0 (0x0). The thread '' (0xfcc) has exited with code 0 (0x0).

A first chance exception of type 'System.AppDomainUnloadedException' occurred in mscorlib.dll

如果您收到 AppDomainUnloadedException,那么您可以忽略它,因为卸载应用程序域时会抛出 AppDomainUnloadedException,而这正是您想要的。

来自msdn

The exception that is thrown when an attempt is made to access an unloaded application domain