Is it possible to somehow hook into the creation process of Task
in TPL?
If I make my own TaskScheduler
, I can get hold of the Task
when it is about to be scheduled for execution.
If I make my own SynchronizationContext
, I can get hold of the continuation when it is about to execute after the Task
is completed.
But is there some way to set up the default task factory to use my special TaskScheduler? Or can I somehow apply my task scheduler to be used for any task created in the current thread?
What I would like to do is something like:
SetTaskSchedulerForCurrentThread(taskScheduler);
// call code that may or may not use async await or normal tasks...
RunStuff();
UnSetTaskSchedulerForCurrentThread();
I've got everything sort of working, I use Task.Factory.StartNew and pass in my custom TaskScheduler. BUT, I don't want the task that I spin up to use that scheduler, just any potential child task.
Am I out of luck or is it possible to solve?