[英]How to make my custom TaskScheduler the default one
I have my own implementation of TaskScheduler
. 我有自己的TaskScheduler
实现。 The main reason for its existence is that it will set processor core affinity to the thread running my task. 它存在的主要原因是它将处理器核心亲和力设置为运行我的任务的线程。
When I use it in the following manner: 当我以下列方式使用它时:
var myTaskSceduler = new MyTaskScheduler(4);
var taskFactory = new TaskFactory(myTaskSceduler);
taskFactory.StartNew(DoSomething);
the affinity works fine, the task will run only on the specified core. 亲和力工作正常,任务将只在指定的核心上运行。
How can I change Task.Factory
or Task.Factory.Scheduler
so my scheduler will be the default one whenever 如何更改Task.Factory
或Task.Factory.Scheduler
以便我的调度程序将成为默认调度程序
Task.Factory.StartNew()
is being called? 被叫?
Why not just create your own static method, MyTask.StartNew()
? 为什么不创建自己的静态方法MyTask.StartNew()
? Don't you have to specify the affinity as a parameter anyway? 你不必将亲和力指定为参数吗?
In any case, the task factory should use the same scheduler you're in when you call StartNew
. 在任何情况下,任务工厂都应使用调用StartNew
时所在的调度程序。 So if you do that on a task on the proper scheduler, it should work just fine. 因此,如果您在正确的调度程序上执行任务,它应该可以正常工作。 This really depends on how you handle your processing scheduling etc, but I assume you're going to be context dependent anyway if you're not going to be passing the affinity anywhere. 这实际上取决于你如何处理你的处理调度等,但我认为如果你不打算在任何地方传递亲和力,你将会依赖于上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.