帮助超出范围的函数(C#)

时间:2011-06-26 17:38:54

标签: c# .net windows

我正在尝试拨打CancelAsync,但webClient超出了范围。

private void Download_Click(object sender, EventArgs e)
{
    WebClient webClient = new WebClient();
}


private void Button1_Click(object sender, EventArgs e)
{
    webClient.CancelAsync();
}

有人可以告诉我如何从这个事件处理程序中调用webClient.CancelAsync()

2 个答案:

答案 0 :(得分:6)

您需要将WebClient存储在班级的字段中。

答案 1 :(得分:3)

class ....
{
    WebClient webClient;

    private void Download_Click(object sender, EventArgs e)
    {
        webClient = new WebClient();
    }


    private void Button1_Click(object sender, EventArgs e)
    {
        webClient.CancelAsync();
    }