It's not built-in, but my AsyncContext
and AsyncContextThread
types are available in a library that would fit your need.
AsyncContext
takes over the current thread:
AsyncContext.Run(async () =>
{
... // any awaits in here resume on the same thread.
});
// `Run` blocks until all async work is done.
AsyncContextThread
is a separate thread with its own AsyncContext
:
using (var thread = new AsyncContextThread())
{
// Queue work to the thread.
thread.Factory.Run(async () =>
{
... // any awaits in here resume on the same thread.
});
await thread.JoinAsync(); // or `thread.Join();`
}
AsyncContext
provides a SynchronizationContext
as well as a TaskScheduler
/TaskFactory
.