AppDomains、卸载和 ThreadAbortException [英] AppDomains, Unloading, and ThreadAbortException

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

问题描述

我第一次尝试使用 AppDomains,但发现自己有点迷茫.

I'm trying to work with AppDomains for the first time and I'm finding myself a bit lost.

这是我所做的:我有一个控制台应用程序,它实例化 Bootstrapper 类并调用 Bootstrapper.Bootstrap.

Here is what I've done: I have a console app that instantiates a Bootstrapper class and calls 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();
        }
    }

目的是无论在辅助域中运行什么,并请求终止并重新启动它.

The intention is that whatever is running in the Secondary domain and request that it be terminated and restarted.

然而,当我调用 DestroySecondaryAppDomain()(从默认的 AppDomain 中)时,我最终遇到了 ThreadAbortExceptions.

What's happening, though, is that when I call DestroySecondaryAppDomain() (from within the default AppDomain) I wind up with ThreadAbortExceptions.

我一直在阅读大量文档,这似乎完全正常.我遇到的困难是为什么我似乎无法在我的默认 AppDomain 中处理它.

I've been reading a bunch of docs and that seems totally normal. The thing that I'm having difficulty with is why I can't seem to deal with it in my default AppDomain.

当卸载辅助 AppDomain 时(在 DestroySecondaryAppDomain 中),我永远无法执行 DoHardReset 中的其余代码.我不明白什么(可能很简单)?

When the secondary AppDomain is unloaded (in DestroySecondaryAppDomain) I never get to execute the rest of the code in DoHardReset. What (probably simple) thing am I failing to understand?

推荐答案

长话短说,该 AppDomain 中仍有代码在执行.需要完全停止,才能无误地成功卸载.

To make a long story short, there was still code executing in that AppDomain. It needed to be completely stopped before it can be successfully unloaded without error.

这篇关于AppDomains、卸载和 ThreadAbortException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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