4

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?

3
  • 8
    All of this shows the XY problem. You may get better answer if you can explain what is the core problem you're trying to solve instead of asking what you thought would be the solution. Nov 28, 2014 at 18:45
  • 5
    It is not good practice to change the scheduler for unrelated code which suspects nothing. Only the piece of code that schedules a task knows where it should run. Why can't you control the scheduler explicitly? Why is this tagged with await?
    – usr
    Nov 28, 2014 at 18:55
  • I want change default Task.Factory.Scheduler because it shit.
    – nim
    Jan 15, 2018 at 21:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.