简体   繁体   English

Thread.Sleep在Asp.net vNext中

[英]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.

相关问题 为什么lock(this)thread.sleep不能与ASP.NET线程一起使用? - Why does lock(this) thread.sleep not work with ASP.NET threading? 在ASP.NET中轮询Google Big Query结果期间调用Thread.Sleep是否很好? 备择方案? - Is it good to call Thread.Sleep during polling Google Big Query results in ASP.NET? Alternatives? 在 ASP.NET Core 7 中编写响应时使用 Thread.Sleep() - Use Thread.Sleep() while writing response in ASP.NET Core 7 有没有办法在不使用Thread.Sleep的情况下延迟ASP.NET MVC 4中的HTTP响应? - Is there a way to delay an HTTP response in ASP.NET MVC 4 without using Thread.Sleep? 在ASP.NET中使用Thread.Sleep(int)是否合理,还是应该使用其他方法? - Is it sane to use Thread.Sleep(int) in ASP.NET or should I use another method? 有没有比ASP.NET API中的Thread.Sleep()更好的方法? 请参阅下面的场景 - Is there a better way than Thread.Sleep() in ASP.NET API? See scenario below asp.net Oracle调优线程睡眠 - asp.net Oracle Tuning Thread Sleep Windows Store中.NET中的Thread.Sleep替换 - Thread.Sleep replacement in .NET for Windows Store Mac上的.NET Core中的Thread.Sleep() - Thread.Sleep() in .NET Core on Mac 为什么.NET内部Hashtable中有Thread.Sleep(1)? - Why there is a Thread.Sleep(1) in .NET internal Hashtable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM