本文整理汇总了C#中System.Threading.ExecutionContext.CreateCopy方法的典型用法代码示例。如果您正苦于以下问题:C# ExecutionContext.CreateCopy方法的具体用法?C# ExecutionContext.CreateCopy怎么用?C# ExecutionContext.CreateCopy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.ExecutionContext
的用法示例。
在下文中一共展示了ExecutionContext.CreateCopy方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WrapBodyDelegate
static BodyDelegate WrapBodyDelegate(ExecutionContext context, BodyDelegate body)
{
return body == null ? (BodyDelegate)null : (write, flush, end, cancellationToken) => ExecutionContext.Run(
context.CreateCopy(),
_ => body(write, WrapFlushDelegate(context, flush), end, cancellationToken),
null);
}
示例2: WrapWriteDelegate
static Func<ArraySegment<byte>, Action<Exception>, TempEnum> WrapWriteDelegate(ExecutionContext context, Func<ArraySegment<byte>, Action<Exception>, TempEnum> write)
{
return (data, callback) =>
{
if (callback == null)
{
return write(data, null);
}
return write(data, ex => ExecutionContext.Run(context.CreateCopy(), state => callback((Exception)state), ex));
};
}
示例3: WrapBodyDelegate
static BodyDelegate WrapBodyDelegate(ExecutionContext context, BodyDelegate body)
{
if (body == null)
{
return null;
}
return (write, end, cancellationToken) => ExecutionContext.Run(
context.CreateCopy(),
_ => body(WrapWriteDelegate(context, write), end, cancellationToken),
null);
}
示例4: CaptureOrComplete
private bool CaptureOrComplete(ref ExecutionContext cachedContext, bool returnContext)
{
bool flag = (base.AsyncCallback != null) || ((this._Flags & StateFlags.CaptureContext) != StateFlags.None);
if ((((this._Flags & StateFlags.CaptureIdentity) != StateFlags.None) && !base.InternalPeekCompleted) && (!flag || SecurityContext.IsWindowsIdentityFlowSuppressed()))
{
this.SafeCaptureIdenity();
}
if (flag && !base.InternalPeekCompleted)
{
if (cachedContext == null)
{
cachedContext = ExecutionContext.Capture();
}
if (cachedContext != null)
{
if (!returnContext)
{
this._Context = cachedContext;
cachedContext = null;
}
else
{
this._Context = cachedContext.CreateCopy();
}
}
}
else
{
cachedContext = null;
}
if (base.CompletedSynchronously)
{
base.Complete(IntPtr.Zero);
return true;
}
return false;
}
示例5: RunInternal
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);
}
}
示例6: CaptureOrComplete
// This must be called right before returning the result to the user. It might call the callback itself,
// to avoid flowing context. Even if the operation completes before this call, the callback won't have been
// called.
//
// Returns whether the operation completed sync or not.
private bool CaptureOrComplete(ref ExecutionContext cachedContext, bool returnContext)
{
GlobalLog.Assert((_flags & StateFlags.PostBlockStarted) != 0, "ContextAwareResult#{0}::CaptureOrComplete|Called without calling StartPostingAsyncOp.", Logging.HashString(this));
// See if we're going to need to capture the context.
bool capturingContext = AsyncCallback != null || (_flags & StateFlags.CaptureContext) != 0;
// Peek if we've already completed, but don't fix CompletedSynchronously yet
// Capture the identity if requested, unless we're going to capture the context anyway, unless
// capturing the context won't be sufficient.
if ((_flags & StateFlags.CaptureIdentity) != 0 && !InternalPeekCompleted && (!capturingContext))
{
GlobalLog.Print("ContextAwareResult#" + Logging.HashString(this) + "::CaptureOrComplete() starting identity capture");
SafeCaptureIdentity();
}
// No need to flow if there's no callback, unless it's been specifically requested.
// Note that Capture() can return null, for example if SuppressFlow() is in effect.
if (capturingContext && !InternalPeekCompleted)
{
GlobalLog.Print("ContextAwareResult#" + Logging.HashString(this) + "::CaptureOrComplete() starting capture");
if (cachedContext == null)
{
cachedContext = ExecutionContext.Capture();
}
if (cachedContext != null)
{
if (!returnContext)
{
_context = cachedContext;
cachedContext = null;
}
else
{
_context = cachedContext.CreateCopy();
}
}
GlobalLog.Print("ContextAwareResult#" + Logging.HashString(this) + "::CaptureOrComplete() _Context:" + Logging.HashString(_context));
}
else
{
// Otherwise we have to have completed synchronously, or not needed the context.
GlobalLog.Print("ContextAwareResult#" + Logging.HashString(this) + "::CaptureOrComplete() skipping capture");
cachedContext = null;
GlobalLog.Assert(AsyncCallback == null || CompletedSynchronously, "ContextAwareResult#{0}::CaptureOrComplete|Didn't capture context, but didn't complete synchronously!", Logging.HashString(this));
}
// Now we want to see for sure what to do. We might have just captured the context for no reason.
// This has to be the first time the state has been queried "for real" (apart from InvokeCallback)
// to guarantee synchronization with Complete() (otherwise, Complete() could try to call the
// callback without the context having been gotten).
DebugProtectState(false);
if (CompletedSynchronously)
{
GlobalLog.Print("ContextAwareResult#" + Logging.HashString(this) + "::CaptureOrComplete() completing synchronously");
base.Complete(IntPtr.Zero);
return true;
}
return false;
}
示例7: WrapFlushDelegate
static Func<Action, bool> WrapFlushDelegate(ExecutionContext context, Func<Action, bool> flush)
{
return drained => drained == null ? flush(null) : flush(() => ExecutionContext.Run(context.CreateCopy(), _ => drained(), null));
}
示例8: WrapWriteDelegate
static Func<ArraySegment<byte>, Action, bool> WrapWriteDelegate(ExecutionContext context, Func<ArraySegment<byte>, Action, bool> write)
{
return (data, callback) => callback == null ? write(data, null) : write(data, () => ExecutionContext.Run(context.CreateCopy(), _ => callback(), null));
}