/// <summary> /// Start the script up in its own application domain. /// </summary> /// <returns>True if the script was started successfully.</returns> private bool StartInAppDomain() { m_appDomain = AppDomain.CreateDomain(Name, null, m_domainSetup); if (m_policy != null) m_appDomain.SetAppDomainPolicy(m_policy); Root.Unloader unloader = new Root.Unloader(this); unloader.UnloadRequest += (message) => new Thread(() => { ErrorString = message; NotifyProblem("Problem while running " + Name + "."); Stop(); }).Start(); Type rootT = typeof(Root); m_root = (Root)m_appDomain.CreateInstanceAndUnwrap(rootT.Assembly.FullName, rootT.FullName); string outcome = m_root.Start(m_assembly, m_class, m_host, m_world, ID, Name, unloader, m_args); if (outcome != null) { KillAppDomain(); ErrorString = outcome; Console.WriteLine("Problem. Outcome = " + outcome); return false; } return true; }
private void KillAppDomain() { m_world.Shutdown(); m_host.Kill(TimeSpan.FromMinutes(1)); if (m_root != null) m_root.Kill(TimeSpan.FromMinutes(1)); if (m_appDomain != null) { try { AppDomain.Unload(m_appDomain); } catch (Exception e) { m_log.Warn("[" + Type + "]: Problem unloading application domain for " + Name + ". " + e.Message); } } m_world = null; m_host = null; m_root = null; m_appDomain = null; }