AppDomains, Unloading, and ThreadAbortException

放肆的年华 提交于 2019-12-11 13:09:03

问题


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

Here is what I've done: I have a console app that instantiates a Bootstrapper class and calls Bootstrapper.Bootstrap.

The class looks something like this:

    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.

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

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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/22594702/appdomains-unloading-and-threadabortexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!