【发布时间】:2012-07-15 20:45:06
【问题描述】:
我是 C# 编程的新手,我正在寻找一个快速的解决方案。我在表单上有 2 个按钮,一个是调用 DownloadFileAsync(),第二个应该取消这个操作。 第一个按钮代码:
private void button1_Click(object sender, EventArgs e)
{
...
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(textBox1.Text), destination);
}
第二个按钮的代码:
private void button2_Click(object sender, EventArgs e)
{
webClient.CancelAsync(); // yes, sure, WebClient is not known here.
}
我正在寻找如何快速解决这个问题的想法(使用第一个函数中的 webClient,在第二个块中)。
【问题讨论】:
-
在方法外声明你的 webClient。
-
它不是私有的,而是方法本地的,并且仅在方法执行时才存在。
标签: c# private-members downloadfileasync