简体   繁体   English

如何在 c# 中的每批请求中保持 5 秒的间隔

[英]how to keep a gap of 5 seconds in every batch of request in c#

I have a written an application in c# that receives a request from another application and sends a request to an external broker via HTTP.我在 c# 中编写了一个应用程序,它接收来自另一个应用程序的请求并通过 HTTP 向外部代理发送请求。 broker accepts only 40 requests at a time. broker 一次只接受 40 个请求。 so I have a config where I mention the count.所以我有一个配置,我提到了计数。 Broker is asking us to send 40 requests in one batch and another 40 in another batch but there should be a gap of 5 seconds. Broker 要求我们在一批中发送 40 个请求,在另一批中发送另外 40 个请求,但应该有 5 秒的间隔。 my question is how to call a method at a specific time .我的问题是如何在特定时间调用方法 Presently 2 batches each with 40 requests go at the same time.目前2批次,每批次40个请求go同时进行。 My Present code looks like this.我的当前代码如下所示。 I need some idea how can I handle this.. Help will be highly appreciated.我需要一些想法如何处理这个..帮助将不胜感激。 thanks.谢谢。

if (requestBatchesByAccount.TryGetValue(locate.account, out BatchRequestInfo batch) == false)
{
    batch = new BatchRequestInfo();
}

batch.locates.Add(locate);
requestBatchesByAccount[locate.account] = batch;

if (batch.locates.Count == maxBatchSize)
{                      
    ThreadPool.QueueUserWorkItem(new WaitCallback(RequestStockLoanBatch), batch);
    requestBatchesByAccount.Remove(locate.account);  
}





             

Add the following code before calling the method:在调用该方法之前添加以下代码:

await Task.Delay(5000);

And add async to the method that contains it.并将async添加到包含它的方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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