Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Dispatcher, for .NET Core?

Tags:

c#

.net

.net-core

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?

like image 633
Paul Knopf Avatar asked Jan 19 '17 17:01

Paul Knopf


People also ask

Can Mono run .NET Core?

NET Core, which natively only allows you to build console apps and web applications, mono allows you to build many application types available in . NET Framework, including GUI-enabled desktop apps. So, if mono can do everything that . NET Core can while .

What is .NET Core in C#?

NET Core is a modular, cross-platform, and open source software development framework that is used to build Windows, Web, and Mobile applications for Windows, Linux and OS X platforms.

What is Dispatch C#?

In simple words, it is a way how the programming language calls a method or a function. In general, it doesn't matter whether the method belongs to an instance or to a class. There are two types of dispatch, static and dynamic.

What is WPF dispatcher?

WPF Dispatcher is associated with the UI thread. The UI thread queues methods call inside the Dispatcher object. Whenever your changes the screen or any event executes, or call a method in the code-behind all this happen in the UI thread and UI thread queue the called method into the Dispatcher queue.


1 Answers

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.

like image 111
Stephen Cleary Avatar answered Sep 17 '22 23:09

Stephen Cleary