简体   繁体   中英

Placing a global HTTP requests per second limit

due to server limitations, I cannot make more than one requests per 3 second, I am using Thread.Sleep() to limit the number of requests I can make. Is there a better way without having to pause the thread? Thanks.

static void main(string[] args)
{
    // getids
    List<string> requestIds = GetMyRequestIds();

    foreach(string requestId in requestIds)
    {
        Thread.Sleep(3000);

        // one request for each Id
        result = FetchStatus(requestId);
    }

}

public Dictionary<string, object> FetchStatus(string requestId)
{
    // build http request and query the server
    // ... requestId... http... etc... read to end
    return results;
}

If your limitation is just one request per 3 seconds you could set up a timer which fires a callback every 3 seconds. As this is executed in a separate thread, it is possible that two long running requests execute simultaneously.

System.Threading.Timer

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