I have a hard time getting my head around this.
I need to execute few methods, each one delayed by an amount of time. So from what I could read, I can use Task.Delay or a Timer as it seems internally, Task.Delay is using a Timer.
So are the two following approach equivalent? If not, what would be the pros and cons of each approach?
What happens to the calling thread for each approach? I really don't want to block the thread. I don't think either of those 2 approaches do though.
1st approach:
public async Task SimulateScenarioAsync()
{
await Task.Delay(1000).ConfigureAwait(false);
await FooAsync.ConfigureAwait(false);
await Task.Delay(2000).ConfigureAwait(false);
await BarAsync.ConfigureAwait(false);
await Task.Delay(500).ConfigureAwait(false);
await StuffAsync.ConfigureAwait(false);
}
2nd approach:
public void SimulateScenario()
{
var timer = new Timer(new TimerCallback(FooAsync), null, 1000, Timeout.Infinite);
}
public void FooAsync(Object obj)
{
// do some stuff
var timer = new Timer(new TimerCallback(BarAsync), null, 2000, Timeout.Infinite);
}
public void BarAsync(Object obj)
{
// do some stuff
var timer = new Timer(new TimerCallback(StuffAsync), null, 500, Timeout.Infinite);
}
public void StuffAsync(Object obj)
{
// do some stuff
}