我第一次尝试和AppDomains合作,我发现自己有点迷失了。
下面是我所做的:我有一个控制台应用程序,它实例化一个Bootstrapper类并调用Bootstrapper.Bootstrap。
这门课看起来如下所示:
public class Bootstrapper : MarshalByRefObject
{
private static AppDomain SecondaryAppDomain;
private Bootstrapper _secondaryDomainBootstrapper;
public Robot CurrentlyRunningRobot;
public Bootstrapper OwningBootstrapper;
public Bootstrapper()
{
}
public void Bootstrap()
{
InitializeSecondaryAppDomain();
RunInSecondaryAppDomain();
}
private void DestroySecondaryAppDomain()
{
AppDomain.Unload(SecondaryAppDomain);
}
private static int initCount = 0;
private static void InitializeSecondaryAppDomain()
{
initCount++;
SecondaryAppDomain = AppDomain.CreateDomain("SecondaryAppDomain" + initCount);
}
private void RunInSecondaryAppDomain()
{
_secondaryDomainBootstrapper =
(Bootstrapper)
SecondaryAppDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
"myNamespace.Bootstrapper");
_secondaryDomainBootstrapper.OwningBootstrapper = this;
_secondaryDomainBootstrapper.Initialize(Args);
}
private void Initialize(string[] args)
{
//Do some stuff...
//Start() returns Task<Robot>
var robot = Initializer.Start();
CurrentlyRunningRobot = robot.Result;
CurrentlyRunningRobot.HardResetRequested += OnHardResetRequested;
robot.Wait();
}
private void DoHardReset()
{
DestroySecondaryAppDomain();
InitializeSecondaryAppDomain();
RunInSecondaryAppDomain();
}
private void OnHardResetRequested(object sender, EventArgs e)
{
OwningBootstrapper.DoHardReset();
}
}其意图是,在二级域中运行的任何内容,并请求终止和重新启动它。
但是,所发生的情况是,当我调用DestroySecondaryAppDomain() (在默认的AppDomain中)时,我最终使用了ThreadAbortExceptions。
我读了一堆文档,这看起来很正常。我遇到的困难是,为什么我似乎无法在默认的AppDomain中处理它。
当卸载辅助AppDomain时(在DestroySecondaryAppDomain中),我将永远无法在DoHardReset中执行其余的代码。我不明白什么(可能很简单)?
发布于 2014-03-28 12:32:34
长话短说,AppDomain中仍然有代码正在执行。它需要完全停止,然后才能顺利卸载而不会出错。
https://stackoverflow.com/questions/22594702
复制相似问题