.NET Dispatcher, for .NET Core?

.net, .net-core, c#

Solution

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`.

Problem

Does something like `Dispatcher` exist for .NET Core? I need to create a thread in .NET Core, and be able to send actions to be invoked on the thread. Also, I'd like to be able to use a `TaskScheduler` that can be used for `async/await` stuff. Does this exist somewhere?

Original source

Related problems