为隐式调度方法显式指定 TaskScheduler
我有以下使用隐式调度的方法:
private async Task FooAsync()
{
await Something();
DoAnotherThing();
await SomethingElse();
DoOneLastThing();
}
但是,从一个特定的调用站点,我希望它在低优先级调度程序而不是默认调度程序上运行:
private async Task BarAsync()
{
await Task.Factory.StartNew(() => await FooAsync(),
...,
...,
LowPriorityTaskScheduler);
}
如何实现这一点?这似乎是一个非常简单的问题,但我完全有心理障碍!
注意:我知道该示例实际上不会编译:)
I have the following method which uses implicit scheduling:
private async Task FooAsync()
{
await Something();
DoAnotherThing();
await SomethingElse();
DoOneLastThing();
}
However, from one particular call-site I want it run on a low-priority scheduler instead of the default:
private async Task BarAsync()
{
await Task.Factory.StartNew(() => await FooAsync(),
...,
...,
LowPriorityTaskScheduler);
}
How do I achieve this? It seems like a really simple ask, but I'm having a complete mental block!
Note: I'm aware the example won't actually compile :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建您自己的
TaskFactory
实例,并使用您想要的调度程序进行初始化。然后在该实例上调用 StartNew :Create your own
TaskFactory
instance, initialized with the scheduler you want. Then callStartNew
on that instance:您可以设置线程优先级,但在线程的方法中:
检查 ThreadPriority 枚举的其他值。
You can set thread-priority, but within the thread's method:
Check other values of
ThreadPriority
enum.