private void UnloadOnThread() { try { // Uncomment to simulate an error in unloading //throw new CannotUnloadAppDomainException("Testing: simulated unload error"); // Uncomment to simulate a timeout while unloading //while (true) ; AppDomain.Unload(_domain); } catch (Exception ex) { // We assume that the tests did something bad and just leave // the orphaned AppDomain "out there". var msg = DomainDetailsBuilder.DetailsFor(_domain, $"Exception encountered unloading application domain: {ex.Message}"); _unloadException = new NUnitEngineException(msg); log.Error(msg); } }
public void Unload() { _unloadThread = new Thread(new ThreadStart(UnloadOnThread)); _unloadThread.Start(); var timeout = TimeSpan.FromSeconds(30); if (!_unloadThread.Join((int)timeout.TotalMilliseconds)) { var msg = DomainDetailsBuilder.DetailsFor(_domain, $"Unable to unload application domain: unload thread timed out after {timeout.TotalSeconds} seconds."); log.Error(msg); Kill(_unloadThread); throw new NUnitEngineUnloadException(msg); } if (_unloadException != null) { throw new NUnitEngineUnloadException("Exception encountered unloading application domain", _unloadException); } }