private void StartShell() { AppDomain domain = null; IntPtr password = default(IntPtr); bool logout = false; try { AppDomainSetup setup = new AppDomainSetup(); setup.ConfigurationFile = UserSessionService.ConfigFilename; setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string assemblyName = "Imi.SupplyChain.UX.Shell"; string instanceTypeName = "Imi.SupplyChain.UX.Shell.ShellApplication"; domain = AppDomain.CreateDomain("login", null, setup, new PermissionSet(PermissionState.Unrestricted)); string securityTokenXml = null; if (_tokenCache.RawToken != null) { securityTokenXml = SecurityTokenCache.Serialize(_tokenCache); } if (UserSessionService.Password != null) { password = SecureStringHelper.GetString(UserSessionService.Password); UserSessionService.Password = null; } object[] args = new object[4] { password, UserSessionService, _loadedEvent, securityTokenXml }; CleanTempFiles(); ThreadPool.QueueUserWorkItem(WaitForShellStartCallback); domain.CreateInstance(assemblyName, instanceTypeName, true, BindingFlags.CreateInstance, null, args, null, null); password = default(IntPtr); logout = domain.GetData("Logout") != null; } catch (ThreadAbortException) { } catch (Exception ex) { EventLog.WriteEntry(StringResources.Title, ex.ToString(), EventLogEntryType.Error); throw; } finally { if (password != default(IntPtr)) { SecureStringHelper.FreeString(password); } if (domain != null) { try { AppDomain.Unload(domain); } catch (CannotUnloadAppDomainException) { } finally { domain = null; } } try { File.Delete(UserSessionService.ConfigFilename); } catch (Exception) { } CleanTempFiles(); if (logout) { _loginWindow.Dispatcher.Invoke(new Action(() => { ShowLogin(true); _loginWindow.Show(); })); } else { _loginWindow.Closing -= LoginWindowClosingEventHandler; _loginWindow.Dispatcher.Invoke(new Action(() => { _loginWindow.Close(); })); } } }