.NET 调度程序,用于 .NET Core?

Pau*_*opf 11 .net c# .net-core

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

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

这在某处存在吗?

Ste*_*ary 7

它不是内置的,但 myAsyncContextAsyncContextThreadtypes可以在满足您需求的库中找到。

AsyncContext接管当前线程:

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

AsyncContextThread是一个单独的线程,有自己的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();`
}
Run Code Online (Sandbox Code Playgroud)

AsyncContext提供 aSynchronizationContext以及TaskScheduler/ TaskFactory


Eri*_*let 6

Just as reference: You can do like both answers of codevision and JBSnorro:

手动编辑 .csproj 文件。项目文件应该类似于以下内容(对于 Core 3.1,使用 3.1 而不是 3.0):

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
        <UseWPF>true</UseWPF>
    </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

如果项目已卸载,请使用内容菜单中的重新加载项目。

注意:应添加 UseWPF 但也应修改“Project Sdk”类型。