【发布时间】:2019-12-16 19:09:59
【问题描述】:
我有一个创建多个对象的 WinForms 应用程序,每个对象都有自己的 WebClient 并在其构造函数上调用 DownloadStringAsync。当回调 DownloadStringAsyncComplete 被调用时,显示被更新。 我想在这里保留的主要方面是能够同时执行多个请求,因为每个请求都需要几秒钟。 如何使用 HttpClient 完成此操作? 我无法等待 GetAsync,但是,我怎么知道每个响应何时到达? 如果我确实等待 GetAsync,那么它们将一个接一个地执行,整个过程将需要很长时间......
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
namespace RequestTest
{
public partial class Form1 : Form
{
List<Requester> requesters;
public Form1()
{
InitializeComponent();
requesters = new List<Requester>();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
requesters.Add(new Requester(this));
}
}
public void SomeoneCompletedDownload()
{
textBox1.AppendText("sample text");
}
}
public class Requester
{
Form1 caller;
public Requester(Form1 _caller)
{
caller = _caller;
var client = new WebClient();
client.DownloadStringCompleted += DownloadCompleted;
client.DownloadStringAsync(new Uri("some-url"));
}
private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
caller.SomeoneCompletedDownload();
}
}
}
【问题讨论】:
-
这能回答你的问题吗? Download multiple files in parallel using c#
-
一架飞机从伦敦飞到纽约需要8小时...如果你有8架飞机需要多少? ...似乎您相信这需要 1 小时 ...
-
@Selvin 确实需要更少的时间,因为大部分时间都花在了服务器端。
-
@Eldar 不是真的。我的应用程序当前可以工作,但根据 Microsoft 的说法,不应使用 WebClient 类,而应使用 HttpClient 。我正在尝试在我的应用中查看如何操作。
标签: c# httpclient webclient