ASP.Net MVC 4 I need to Delay execution

11,955

Solution 1

You shouldn't use Thread.Sleep because it blocks the thread for that amount of time so your server is less scalable. You should instead use Task.Delay which waits asynchronously without blocking a thread:

await Task.Delay(10000)

Task.Delay uses a Timer internally to accomplish that. For more info: Thread.Sleep vs Task.Delay?

Solution 2

I recommend you use the Transient Fault Handling Block, which was designed specifically for this kind of scenario.

However, if you want to implement your own retry mechanism, Task.Delay is a better choice than Task.Run+Thread.Sleep.

Solution 3

25 seconds is a lot of time to wait for the response on the client-side, while you're delaying the 3rd party API request on the server. It might be a good idea to initiate the retry from the client AJAX code, if you really need this data/resource on the client-side. Then you wouldn't have to organize the wait on the server-side at all.

Solution 4

Since it appears you are doing this in ASP.NET, then you could use Quartz.net to schedule jobs and change when a job needs to execute again (say 25 seconds). Their website: Quartz Enterprise Scheduler .NET.

A previous question on using Quartz.net in ASP.NET is located here. Hope that helps you out.

Share:
11,955
hjavaher
Author by

hjavaher

Updated on June 14, 2022

Comments

  • hjavaher
    hjavaher almost 2 years

    I have an ASP.Net MVC 4 application that periodically calls an external API for information (resource). This resource has a rate limiter for the account (meaning other apps use the same pool and may hit the limit). When this limit is hit, It will send back a HTTP Status Code 429 with a header of "Retry-After" in seconds (lets say 25 seconds).

    If my app gets this response, I will then need to delay execution for 25 Seconds and retry. First off, let me say that the method that this code is running under is an ASP.Net 4.5 Async method. For this, I was thinking about using the System.Threading.Thread.Sleep(25000) Now, I really don't like to use this, is there a better way of doing this?

    I have to say that I apologize for this open ended question, but I couldn't find anything on the proper way of delay execution (while keeping things async and making sure that we don't run out of threads)

    Update: Would the following code be better for the delay?

      await Task.Run(() => Thread.Sleep(10000))