简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM