I have the following method which uses implicit scheduling:
private async Task FooAsync()
{
await Something();
DoAnotherThing();
await SomethingElse();
DoOneLastThing();
}
However, from one particular call-site I want it run on a low-priority scheduler instead of the default:
private async Task BarAsync()
{
await Task.Factory.StartNew(() => await FooAsync(),
...,
...,
LowPriorityTaskScheduler);
}
How do I achieve this? It seems like a really simple ask, but I'm having a complete mental block!
Note: I'm aware the example won't actually compile :)
Task scheduler interface that abstracts the scheduling of Runnables based on different kinds of triggers. This interface is separate from SchedulingTaskExecutor since it usually represents for a different kind of backend, i.e. a thread pool with different characteristics and capabilities.
4.1. Let's schedule a task to run at a fixed rate of milliseconds: taskScheduler. scheduleAtFixedRate( new RunnableTask("Fixed Rate of 2 seconds") , 2000); The next RunnableTask will always run after 2000 milliseconds, regardless of the status of the last execution, which may still be running.
Create your own TaskFactory
instance, initialized with the scheduler you want. Then call StartNew
on that instance:
TaskScheduler taskScheduler = new LowPriorityTaskScheduler();
TaskFactory taskFactory = new TaskFactory(taskScheduler);
...
await taskFactory.StartNew(FooAsync);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With