AsyncMethodBuilder Start How to Capture Execution Context? #56202
-
In the internal AsyncMethodBuilderCore, the Thread currentThread = Thread.CurrentThread;
ExecutionContext? previousExecutionCtx = currentThread._executionContext;
SynchronizationContext? previousSyncCtx = currentThread._synchronizationContext;
try
{
stateMachine.MoveNext();
}
finally
{
if (previousSyncCtx != currentThread._synchronizationContext)
{
currentThread._synchronizationContext = previousSyncCtx;
}
ExecutionContext? currentExecutionCtx = currentThread._executionContext;
if (previousExecutionCtx != currentExecutionCtx)
{
ExecutionContext.RestoreChangedContextToThread(currentThread, previousExecutionCtx, currentExecutionCtx);
}
} I'm looking to implement this logic in my own custom AsyncMethodBuilder, but the code there is using internals that I don't have access to without reflection. How can I properly implement that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Use ExecutionContext.Run, or use the Start method of a temporary AsyncTaskMethodBuilder, ala https://github.com/dotnet/corefx/blob/a0e326571517f03e8d6ccaf88b250b9b45961fff/src/Common/src/CoreLib/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs#L42 |
Beta Was this translation helpful? Give feedback.
Use ExecutionContext.Run, or use the Start method of a temporary AsyncTaskMethodBuilder, ala https://github.com/dotnet/corefx/blob/a0e326571517f03e8d6ccaf88b250b9b45961fff/src/Common/src/CoreLib/System/Runtime/CompilerServices/AsyncValueTaskMethodBuilder.cs#L42