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

本文关键字:函数 范围 处理 帮助 | 更新日期: 2023-09-27 18:00:30

尽管webClient超出范围,但我正在尝试调用CancelAsync

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

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

有人能告诉我如何从这个事件处理程序调用webClient.CancelAsync()吗?请

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

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

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

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