.NET 调度程序,用于 .NET Core? [英] .NET Dispatcher, for .NET Core?

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

问题描述

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

Does something like Dispatcher exist for .NET Core?

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

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.

这在某处存在吗?

推荐答案

它不是内置的,而是我的 AsyncContextAsyncContextThread 类型 可在满足您需要的库中使用.

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.

AsyncContext provides a SynchronizationContext as well as a TaskScheduler/TaskFactory.

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

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