卸载和ThreadAbortException

卸载和ThreadAbortException

本文介绍了AppDomain,卸载和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.

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

08-20 06:30