public static void Run(ExecutionContext executionContext, ContextCallback callback, Object state) { if (executionContext == null) { throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullContext")); } Thread currentThread = Thread.CurrentThread; ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher); try { EstablishCopyOnWriteScope(currentThread, ref ecsw); ExecutionContext.Restore(currentThread, executionContext); callback(state); } catch { // Note: we have a "catch" rather than a "finally" because we want // to stop the first pass of EH here. That way we can restore the previous // context before any of our callers' EH filters run. That means we need to // end the scope separately in the non-exceptional case below. ecsw.Undo(currentThread); throw; } ecsw.Undo(currentThread); }
internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx) { if (!executionContext.IsPreAllocatedDefault) { executionContext.isNewCapture = false; } Thread currentThread = Thread.CurrentThread; ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher(); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader(); if ((executionContextReader.IsNull || executionContextReader.IsDefaultFTContext(preserveSyncCtx)) && (SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextReader) && executionContext.IsDefaultFTContext(preserveSyncCtx)) && executionContextReader.HasSameLocalValues(executionContext)) { ExecutionContext.EstablishCopyOnWriteScope(currentThread, true, ref ecsw); } else { if (executionContext.IsPreAllocatedDefault) { executionContext = new ExecutionContext(); } ecsw = ExecutionContext.SetExecutionContext(executionContext, preserveSyncCtx); } callback(state); } finally { ecsw.Undo(); } }
internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx) { Contract.Assert(executionContext != null); if (executionContext.IsPreAllocatedDefault) { Contract.Assert(executionContext.IsDefaultFTContext(preserveSyncCtx)); } else { Contract.Assert(executionContext.isNewCapture); executionContext.isNewCapture = false; } Thread currentThread = Thread.CurrentThread; ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher); RuntimeHelpers.PrepareConstrainedRegions(); try { ExecutionContext.Reader ec = currentThread.GetExecutionContextReader(); if ((ec.IsNull || ec.IsDefaultFTContext(preserveSyncCtx)) && #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK SecurityContext.CurrentlyInDefaultFTSecurityContext(ec) && #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK executionContext.IsDefaultFTContext(preserveSyncCtx)) { // Neither context is interesting, so we don't need to set the context. // We do need to reset any changes made by the user's callback, // so here we establish a "copy-on-write scope". Any changes will // result in a copy of the context being made, preserving the original // context. EstablishCopyOnWriteScope(currentThread, true, ref ecsw); } else { if (executionContext.IsPreAllocatedDefault) { executionContext = executionContext.CreateCopy(); } ecsw = SetExecutionContext(executionContext, preserveSyncCtx); } // // Call the user's callback // callback(state); } finally { ecsw.Undo(currentThread); } }
public static void Run(ExecutionContext executionContext, ContextCallback callback, Object state) { ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher); try { EstablishCopyOnWriteScope(ref ecsw); ExecutionContext.Restore(executionContext); callback(state); } catch { // Note: we have a "catch" rather than a "finally" because we want // to stop the first pass of EH here. That way we can restore the previous // context before any of our callers' EH filters run. That means we need to // end the scope separately in the non-exceptional case below. ecsw.Undo(); throw; } ecsw.Undo(); }