Asynchronous And Parallel Programming In .Net – Part 3 – When Should You Use Task.Delay

This post is about how Task.Delay can be used in different scenarios. Generally speaking we use Task.Delay to wait for specific amount of time in asynchronous fashion. I’m also going to explain why do we need to sometimes mimic this kind of behaviors.

What Task.Delay Does?

In some scenarios, we need to wait for specific amount of time and try something again. This usually happens when we want to create an asynchronous method for test. Or we need to wait for specific amount of time and retry calling a service that didn’t response on first attempt. By the way this is called exponential backoff and there’s a complete library to handle this kind of situations. One of these libraries is polly, which is the recommended approach to handle this. Here’s a good article of its usage. In the next section we’re going to see some practical usage for Task.Delay. Worth to note that what I discuss here about exponential back off with Task.Delay is very simplistic and shouldn’t be used in this form in production.

Creating A Fake Asynchronous Operation For Test

In my previous post about async and await, I needed to mimic an asynchronous operation.

In this case I needed the SaySomething method to wait asynchronously. By doing that I saw how thread is released when we reach the await keyword. It can be a useful mechanism for us to mimic async operation and see how our program behave.

Implementing Exponential Back off

Most of the time operations such as network related ones might fail and its not in our control. This failure can be due to a busy network or some other issue, but the important thing is, it’s not going to stay like this. Maybe if we issue the same request again after one minute, the operation result in success. So we need some kind of mechanism to retry the same request. We can do it with Tread.Sleep like so.

The problem with this approach is it wait for 2 second and during this time the thread is idle. That mean the task scheduler doesn’t going to use this thread for any operation. This doesn’t consume any CPU but it can potentially waste memory resources. A better approach is to release the thread during the time of waiting.

By using Task.Delay, now when we wait to retry the request, the thread is release back to its caller or to thread pool. Another potential scenario is when we want to do something but within a specific time frame. If the task is not finished by that time, We’re going to return null.

Task.Delay vs Thread.Sleep

We can use Thread.Sleep, but the problem with Thread.Sleep is during the time that Tread is Sleeping, it cannot be used for another task. Wasting thread dose not necessarily means that we are wasting CPU time, but there a cost for creating new threads and keeping an unused thread alive. This is not important in desktop applications, because there’s a lot of memory available, but in servers, conserving RAM is very important. So instead of Thread.Sleep we can use Task.Delay. By doing that when we wait, the thread can be used for other tasks. Which means while we’re waiting, the thread is released back to its caller or thread pool and no resource is wasted in the process.

Summary

In this post I discussed What Task.Delay does and what can it do for us. I also explained how Task.Delay is different from Tread.Sleep and what is the consequence of using it.

Did you like this?
Tip Hamid Mosalla with Cryptocurrency

Donate Bitcoin to Hamid Mosalla

Scan to Donate Bitcoin to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to Hamid Mosalla

Scan to Donate Bitcoin Cash to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to Hamid Mosalla

Scan to Donate Ethereum to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to Hamid Mosalla

Scan to Donate Litecoin to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to Hamid Mosalla

Scan to Donate Monero to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to Hamid Mosalla

Scan to Donate ZCash to Hamid Mosalla
Scan the QR code or copy the address below into your wallet to send some ZCash:
Share...
 

Hamid Mosalla

Hi, I'm Hamid Mosalla, I'm a software developer, indie cinema fan and a classical music aficionado. Here I write about my experiences mostly related to web development and .Net.