Consider the following code that loops until a precondition is satisfied. The call to CheckPrecondition()
is not doing any resource-intensive work, it is simply checking a boolean flag which is set to true on a separate thread.
public async Task LoopUntilPreconditionIsSatisfied()
{
bool preconditionIsSatisfied = false;
while (!preconditionIsSatisfied)
{
var preconditionIsSatisfied = CheckPrecondition();
// Is there a performance benefit to delaying this task?
// How long should the delay be? Is it pointless if it's too short?
await Task.Delay(10);
}
}
Does adding await Task.Delay()
meaningfully improve performance by releasing the thread to do other work?
Is there a recommended delay time? I want it to be long enough to actually be useful while short enough to react quickly to the precondition being satisfied. Obviously I could make it configurable but I would at least like to have a sensible default.