.NET分派器,用于.NET Core? [英] .NET Dispatcher, for .NET Core?

查看:90
本文介绍了.NET分派器,用于.NET Core?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET Core是否存在类似 Dispatcher 的东西?

Does something like Dispatcher exist for .NET Core?

我需要在.NET中创建一个线程核心,并能够发送要在线程上调用的动作。另外,我希望能够使用 TaskScheduler 可以用于 async / await 的东西。

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.

这是否存在?

推荐答案

它不是内置的,但是我的 AsyncContext AsyncContextThread 类型在满足您需求的库中可用。

It's not built-in, but my AsyncContext and AsyncContextThread types are available in a library that would fit your need.

AsyncContext 接管当前线程:

AsyncContext.Run(async () =>
{
  ... // any awaits in here resume on the same thread.
});
// `Run` blocks until all async work is done.

AsyncContextThread 是具有自己的独立线程 AsyncContext

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 提供 SynchronizationContext 以及 TaskScheduler / TaskFactory

这篇关于.NET分派器,用于.NET Core?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆