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.