[英]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. 我需要调用一个JSON api,它只允许每秒有限数量的请求,以确保不会从特定的IP发生DDOS。 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.
请求需要多长时间并不重要,因为这将在一夜之间作为自动化过程完成,但是我需要在每个请求之间暂停某些方式以确保我的IP不会暂时列入黑名单。
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. 现在我要完成Thread.Sleep(1000)以确保只发送一个请求,但在asp.net vNext Thread.Sleep不起作用/存在所以我不能这样做。
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.
没有批量API,因此必须一次完成1个请求。
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. 另请注意, 等待Task.Delay使您的执行线程可以被其他工作重用,但Thread.Sleep会阻塞。
You're missing a dependency. 你错过了一个依赖。 If your code is not async, you shouldn't use
Task.Delay
... you should use Thread.Sleep()
or normal. 如果您的代码不是异步,则不应使用
Task.Delay
...您应该使用Thread.Sleep()
或normal。
Just add the following dependency: 只需添加以下依赖项:
"System.Threading.Thread": "4.0.0-beta-23516"
Then you can use Thread.Sleep
as normal. 然后你可以照常使用
Thread.Sleep
。
Of course, if you are writing async code, then await Task.Delay(...)
is appropriate - again, just like normal. 当然,如果您正在编写异步代码,那么
await Task.Delay(...)
是合适的 - 再次,就像正常一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.