访问另一个类中的DownloadStringAsync
本文关键字:DownloadStringAsync 另一个 访问 | 更新日期: 2023-09-27 18:17:09
我正在使用web客户端从类内进行异步下载。即
public void download()
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://www.url.com"));
}
我正在尝试使用CancelAysnc
方法,我想我会使用类似的东西:
client.CancelAsync();
但是我想使用它,例如,一个点击事件方法。当然,当我尝试使用上面的例子,它不知道客户端。我怎样才能使用它?
谢谢
您必须将客户端对象存储在局部变量之外的其他地方。这样,您就可以在download()
方法之外访问它。
Scope…
WebClient client;
public void download() {
client = new WebClient();
// Further code...
}
public void cancel() {
client.CancelAsync();
}