I have recently seen several recommendations stating that Thread.Sleep
should never be used in production code (most recently in this SO question). Many of these advocate for using Task.Delay
instead. Most of the explanations I've found use UI applications as examples, since the advantages to Task.Delay
are obvious (not blocking the UI).
In my case, I am using Thread.Sleep
inside of a wait loop that polls a WCF service for a particular condition, like this:
DateTime end = DateTime.UtcNow + TimeSpan.FromMinutes(2);
while (DateTime.UtcNow < end)
{
if (ExternalServiceIsReady() == true)
{
return true;
}
Thread.Sleep(1000);
}
In this case, the following potential advantages of Task.Delay
seem not to apply:
Task.Delay
seems trivial.await
has no advantage here.Is this a case where it is appropriate to use Thread.Sleep
? What would be the advantage (if any) of replacing my sleep line with Task.Delay(1000).Wait()
?
There's never an advantage in replacing Thread.Sleep(1000);
in Task.Delay(1000).Wait();
. If you want to wait synchronously just use Thread.Sleep
.
If you really only have a single thread and planning to keep it that way, then you can use Thread.Sleep
. However, I would still use Task.Delay
as it's preferable in most cases and so it's a good pattern. I would only block at very top when you can't use async
anymore, and even then I would suggest using some kind of AsyncContext
.
You can also use a System.Threading.Timer
directly* instead of Task.Delay
however you should keep in mind that the timer executes every interval and doesn't wait for the actual operation to complete, so if ExternalServiceIsReady
takes more than the interval you can have multiple calls to that service concurrently.
An even better solution would be to replace the polling of the external service with an asynchronous operation so the service can notify you when it's ready instead of you asking it every second (that isn't always possible as it depends on the service):
await ExternalServiceIsReadyAsync();
*Task.Delay
uses a System.Threading.Timer
internally which also has a resolution of ~15ms.
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