gpt4 book ai didi

c# - 如何使用 async 和 await 发出大量并发 Web 请求?

转载 作者:太空狗 更新时间:2023-10-29 22:21:19 28 4
gpt4 key购买 nike

我阅读了 Microsoft 的操作方法,网址为 How to: Make Multiple Web Requests in Parallel by Using async and await (C#)并发现:

private async Task CreateMultipleTasksAsync()  
{
// Declare an HttpClient object, and increase the buffer size. The
// default buffer size is 65,536.
HttpClient client =
new HttpClient() { MaxResponseContentBufferSize = 1000000 };

// Create and start the tasks. As each task finishes, DisplayResults
// displays its length.
Task<int> download1 =
ProcessURLAsync("http://msdn.microsoft.com", client);
Task<int> download2 =
ProcessURLAsync("http://msdn.microsoft.com/library/hh156528(VS.110).aspx", client);
Task<int> download3 =
ProcessURLAsync("http://msdn.microsoft.com/library/67w7t67f.aspx", client);

// Await each task.
int length1 = await download1;
int length2 = await download2;
int length3 = await download3;

int total = length1 + length2 + length3;

// Display the total count for the downloaded websites.
resultsTextBox.Text +=
string.Format("\r\n\r\nTotal bytes returned: {0}\r\n", total);
}

我理解这段代码,但我的问题是:我如何修改它以将其缩放到喜欢的程度,比方说一百或一千?

最佳答案

由于我的帐户限制,我无法发表评论,但回复@CreativeManix 的回答,

List<Task> tasks = new List<Task>();

Task.WaitAll(tasks)将不接受任务列表。其中一个覆盖接受一组任务。

Task.WaitAll(tasks.ToArray())

关于c# - 如何使用 async 和 await 发出大量并发 Web 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055516/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com