Exemplo n.º 1
0
        public void DisposeAppDomain()
        {
            log.LogMessage(string.Format("Shutting down Appdomain for Task {0}", TaskId));
            StopExecutorMonitoringThread();

            if (executor != null)
            {
                try {
                    executor.Dispose();
                }
                catch (Exception ex) {
                    EventLogManager.LogException(ex);
                }
            }

            if (appDomain != null)
            {
                appDomain.UnhandledException -= new UnhandledExceptionEventHandler(AppDomain_UnhandledException);
                int repeat = Settings.Default.PluginDeletionRetries;
                while (repeat > 0)
                {
                    try {
                        waitForStartBeforeKillSem.WaitOne(Settings.Default.ExecutorSemTimeouts);
                        AppDomain.Unload(appDomain);
                        waitForStartBeforeKillSem.Dispose();
                        repeat = 0;
                    }
                    catch (CannotUnloadAppDomainException) {
                        log.LogMessage("Could not unload AppDomain, will try again in 1 sec.");
                        Thread.Sleep(Settings.Default.PluginDeletionTimeout);
                        repeat--;
                        if (repeat == 0)
                        {
                            throw; // rethrow and let app crash
                        }
                    }
                }
            }
            pluginManager.DeletePluginsForJob(TaskId);
            GC.Collect();
        }