Question

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.

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.StartNew()

is being called?

Was it helpful?

Solution

Why not just create your own static method, 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top