How to back up the default TaskScheduler job from a custom TaskScheduler

Let's say I want to create a custom TaskScheduler, but inside it will revert to default if some criteria are not applied. How to do it?

eg.

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? */);
}
+4
source share
1 answer

I do not think that you can (or should) do this, even if you resort to reflection. Theoretically, you need to get your own TaskSchedulerfrom an existing non-abstract task scheduler, for example. ThreadPoolTaskScheduler.

However, you cannot, because all non-abstract implementations TaskScheduler in TPL are privateor internal.

TaskScheduler.QueueTask 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);
}

. Task.Start TaskScheduler.QueueTask , .

, Task Task.Start. , , ThreadPoolTaskScheduler.QueueTask, .

, , QueueTask, GetScheduledTasks TaskScheduler protected. , .

+2

Source: https://habr.com/ru/post/1533006/


All Articles