Pausing to catch your breath in C#

Sometimes in your code, you find the need to try an operation that’s dependent upon some other system.  That other system might be a little laggy.  As such, you want to try your operation and then pause a moment or two before you try again.  A simple way to do this is with


That little snippet of code will wait for one second and then continue.  There’s a better way though…

You see… sleeping your thread is a blocking call.  It blocks everything while that thread is paused.  Your UI is unresponsive and there may be other consequences.  A better way to pause it to await a task delay.

await Task.Run(() => Task.Delay(1000));

These two commands to essentially the same thing for you, but awaiting the task will allow other processes to continue resulting in a more polished product.

Leave a Reply

Scroll To Top