Reputation: 195
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
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
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