public void Undo() { if (thread == null) { return; // Don't do anything } if (thread != Thread.CurrentThread) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread")); } if (currEC != Thread.CurrentThread.GetExecutionContextNoCreate()) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_SwitcherCtxMismatch")); } BCLDebug.Assert(currEC != null, " ExecutionContext can't be null"); // Any critical failure inside scsw will cause FailFast scsw.Undo(); try { HostExecutionContextSwitcher.Undo(hecsw); } finally { // Even if HostExecutionContextSwitcher.Undo(hecsw) throws, we need to revert // synchronizationContext. If that throws, we'll be throwing an ex during an exception // unwind. That's OK - we'll just have nested exceptions. sysw.Undo(); } // restore the saved Execution Context Thread.CurrentThread.SetExecutionContext(prevEC); thread = null; // this will prevent the switcher object being used again }