I'm trying to build a library that internally executes task in an exclusive TaskScheduler built as following:
schedulerPair = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, 1);
simulationFactoryTask = new TaskFactory(schedulerPair.ExclusiveScheduler);
I use the System.Threading.Timer
class:
timer = new Timer(Tick, this, startTime, deltaTime);
That periodically calls my Tick
method:
static void Tick(object? state)
{
var self = state as MyTimerScheduler;
// fake computation
Thread.Sleep(10_000);
}
The issue is that the Tick
method is executed inside a different thread and not in the one inside my schedulerPair.
One solution is to schedule a new Task like that:
static void Tick(object? state)
{
var self = state as MyTimerScheduler;
// This shouldn't be needed
self.simulationFactoryTask.StartNew(() =>
{
// fake computation
Thread.Sleep(10_000);
});
}
Is it possible to schedule Tick
inside schedulerPair TaskScheduler
?