public void Undo() { if (this.thread != null) { if (this.thread != Thread.CurrentThread) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread")); } if (this.currEC != Thread.CurrentThread.GetExecutionContextNoCreate()) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_SwitcherCtxMismatch")); } this.scsw.Undo(); try { HostExecutionContextSwitcher.Undo(this.hecsw); } finally { this.sysw.Undo(); } Thread.CurrentThread.SetExecutionContext(this.prevEC); this.thread = null; } }
internal void Undo() { if (this.thread == null) { return; } Thread thread = this.thread; if (this.hecsw != null) { HostExecutionContextSwitcher.Undo(this.hecsw); } ExecutionContext.Reader executionContextReader = thread.GetExecutionContextReader(); ExecutionContext.Reader reader = this.outerEC; int num = this.outerECBelongsToScope ? 1 : 0; thread.SetExecutionContext(reader, num != 0); if (this.scsw.currSC != null) { this.scsw.Undo(); } if (this.wiIsValid) { SecurityContext.RestoreCurrentWI(this.outerEC, executionContextReader, this.wi, this.cachedAlwaysFlowImpersonationPolicy); } this.thread = (Thread)null; ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), this.outerEC.DangerousGetRawExecutionContext()); }
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 }
internal void Undo(Thread currentThread) { // // Don't use an uninitialized switcher, or one that's already been used. // if (thread == null) { return; // Don't do anything } Contract.Assert(currentThread == this.thread); // // Restore the HostExecutionContext before restoring the ExecutionContext. // #if FEATURE_CAS_POLICY if (hecsw != null) { HostExecutionContextSwitcher.Undo(hecsw); } #endif // FEATURE_CAS_POLICY // // restore the saved Execution Context. Note that this will also restore the // SynchronizationContext, Logical/IllogicalCallContext, etc. // #if !FEATURE_PAL && FEATURE_IMPERSONATION ExecutionContext.Reader innerEC = currentThread.GetExecutionContextReader(); #endif currentThread.SetExecutionContext(outerEC, outerECBelongsToScope); #if DEBUG try { currentThread.ForbidExecutionContextMutation = true; #endif // // Tell the SecurityContext to do the side-effects of restoration. // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK if (scsw.currSC != null) { // Any critical failure inside scsw will cause FailFast scsw.Undo(); } #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK #if !FEATURE_PAL && FEATURE_IMPERSONATION if (wiIsValid) { SecurityContext.RestoreCurrentWI(outerEC, innerEC, wi, cachedAlwaysFlowImpersonationPolicy); } #endif thread = null; // this will prevent the switcher object being used again #if DEBUG } finally { currentThread.ForbidExecutionContextMutation = false; } #endif }