如何使我的自定义TaskScheduler成为默认值

时间:2022-07-05 00:19:55

I have my own implementation of TaskScheduler. The main reason for its existence is that it will set processor core affinity to the thread running my task.

我有自己的TaskScheduler实现。它存在的主要原因是它将处理器核心亲和力设置为运行我的任务的线程。

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?

被叫?

1 个解决方案

#1


0  

Why not just create your own static method, MyTask.StartNew()? Don't you have to specify the affinity as a parameter anyway?

为什么不创建自己的静态方法MyTask.StartNew()?你不必将亲和力指定为参数吗?

In any case, the task factory should use the same scheduler you're in when you call 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.

在任何情况下,任务工厂都应使用调用StartNew时所在的调度程序。因此,如果您在适当的调度程序上执行任务,它应该可以正常工作。这实际上取决于你如何处理你的处理调度等,但我认为如果你不打算在任何地方传递亲和力,你将会依赖于上下文。

#1


0  

Why not just create your own static method, MyTask.StartNew()? Don't you have to specify the affinity as a parameter anyway?

为什么不创建自己的静态方法MyTask.StartNew()?你不必将亲和力指定为参数吗?

In any case, the task factory should use the same scheduler you're in when you call 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.

在任何情况下,任务工厂都应使用调用StartNew时所在的调度程序。因此,如果您在适当的调度程序上执行任务,它应该可以正常工作。这实际上取决于你如何处理你的处理调度等,但我认为如果你不打算在任何地方传递亲和力,你将会依赖于上下文。