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?
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? */);
}
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, eg 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.