Andrey Shchekin
Andrey Shchekin

Reputation: 21599

How do I fallback to the Default TaskScheduler from a custom TaskScheduler

Let's say I want to create a custom TaskScheduler, but inside it, fall back to the default if some criteria do not apply. How do I do that?

E.g.

protected override void QueueTask(Task task) {
   if (/* some condition */) {
       /* schedule task manually, does not matter */
   }

   /* queue task on default -- how? */
}

protected override IEnumerable<Task> GetScheduledTasks() {
    return Enumerable.Concat(/* my tasks */, /* tasks from default -- how? */);
}

Upvotes: 4

Views: 524

Answers (1)

noseratio
noseratio

Reputation: 61686

I don't think you can (or should) do this, even if you resorted to reflection hacks. In theory, you'd need to derive your custom TaskScheduler from existing non-abstract task scheduler, e.g. ThreadPoolTaskScheduler.

However, you can't because all non-abstract implementations of TaskScheduler in TPL are either private or internal.

In case with TaskScheduler.QueueTask, it might be tempting to simply forward the call to Task.Start:

protected override void QueueTask(Task task) {
   if (/* some condition */) {
       /* schedule task manually, does not matter */
   }

   /* queue task on default -- how? */
   task.Start(TaskScheduler.Default);
}

However, you can not do this, either. The implementation of Task.Start calls TaskScheduler.QueueTask internally, which would result in infinite recursion.

Moreover, the Task object stores a reference to the task scheduler inside Task.Start. So, say, if you used reflection to call ThreadPoolTaskScheduler.QueueTask, it would break the connection between the task and its task scheduler.

So, there's a good reason that QueueTask, GetScheduledTasks and some other methods of TaskScheduler are declared as protected. If you're into implementing a custom task scheduler, you have to provide the full implementation.

Upvotes: 2

Related Questions