如何检查的AppDomain被卸载? [英] How to check if AppDomain is unloaded?

查看:257
本文介绍了如何检查的AppDomain被卸载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用了一堆物体的通过代理另一个AppDomain中。他们是在一个单独的领域,因为我需要包含这些对象的热插拔组件,所以我卸载一个AppDomain我用它承载组件完成后。

I'm using a bunch of objects in another AppDomain through proxy. They are in a separate domain because I need to hot-swap assemblies that contain those objects, so I Unload an AppDomain after I'm done using the assemblies it's hosting.

我想有时检查,如果我已经在过去卸载一个AppDomain(或莫名其妙地卸载它自己或某事),用于测试目的。有没有办法做到这一点?

I want to check sometimes if I've unloaded an AppDomain in the past (or it somehow got unloaded on its own or something) for testing purposes. Is there a way to do this?

最显而易见的方法就是做东西会抛出一个 AppDomainUnloadedException ,但我希望有一些其他的方式。

The obvious way is to do something that would throw an AppDomainUnloadedException, but I'm hoping there is some other way.

推荐答案

我相信你可以使用存储的参考组合的AppDomain 在某些特定词典< AppDomain中,布尔> 其中布尔是如果加载或卸载,并处理 AppDomain.DomainUnload 事件。

I believe that you can use a combination of storing references of AppDomain in some given Dictionary<AppDomain, bool> where the bool is if its loaded or unloaded, and handle AppDomain.DomainUnload event.

Dictionary<AppDomain, bool> appDomainState = new Dictionary<AppDomain, bool>();

AppDomain appDomain = ...; // AppDomain creation
appDomain.DomainUnload += (sender, e) => appDomainState[appDomain] = false;
appDomainState.Add(appDomain, true);



这样,您就可以检查是否有的AppDomain 卸载:

// "false" is "unloaded"
if(!appDomainState[appDomainReference]) 
{
}

另外,你可以使用 AppDomain.Id 的关键:

Alternatively, you can use AppDomain.Id as key:

Dictionary<int, bool> appDomainState = new Dictionary<int, bool>();

AppDomain appDomain = ...; // AppDomain creation
appDomain.DomainUnload += (sender, e) => appDomainState[appDomain.Id] = false;
appDomainState.Add(appDomain.Id, true);



...和if语句将如下所示:

...and the if statement would look as follows:

// "false" is "unloaded"
if(!appDomainState[someAppDomainId]) 
{
}

这篇关于如何检查的AppDomain被卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆