简体   繁体   English

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

[英]Help with a function that is going out of scope (C#)

I am trying to call CancelAsync , although webClient is out of scope. 我试图调用CancelAsync ,虽然webClient超出范围。

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


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

Could someone show me how webClient.CancelAsync() could be called from this event handler please 有人可以告诉我如何从这个事件处理程序调用webClient.CancelAsync()

您需要将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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM