Jack
Jack

Reputation: 195

Thread.Sleep in Asp.net vNext

I need to make a call to a JSON api which only allows a limited number of requests per second to ensure no DDOS is happening from a specific IP. It doesn't matter how long the requests take, as this will be done as an automated process overnight, however I need some way of pausing between each request to ensure my IP isn't temporarily blacklisted.

Now before I would of done Thread.Sleep(1000) to ensure only 1 request a second is sent, but in asp.net vNext Thread.Sleep doesn't work/exist so I cannot do this.

Is there an alternative or better way to handle what I need to do? There is no bulk API so it has to be done 1 request at a time.

Thanks in advance.

Upvotes: 4

Views: 5229

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1501163

You're missing a dependency. If your code is not async, you shouldn't use Task.Delay... you should use Thread.Sleep() or normal.

Just add the following dependency:

"System.Threading.Thread": "4.0.0-beta-23516"

Then you can use Thread.Sleep as normal.

Of course, if you are writing async code, then await Task.Delay(...) is appropriate - again, just like normal.

Upvotes: 3

mehmet mecek
mehmet mecek

Reputation: 2685

await System.Threading.Tasks.Task.Delay(1000) 

or

System.Threading.Tasks.Task.Delay(1000).Wait()

Also note that, await Task.Delay makes your executing thread reusable by other works, however Thread.Sleep blocks.

Upvotes: 13

Related Questions