2011-06-26 7 views
3

は範囲外ですが、CancelAsyncに電話しようとしています。スコープ外(C#)になる機能のヘルプ

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


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

は、誰かがwebClient.CancelAsync()は、このイベントハンドラから呼び出すことができる方法を見せてもらえてください

答えて

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(); 
    } 
+0

@James Buttler:ようこそ... –

+4

ユーザーがキャンセルせずにDownloadButtonを2回クリックすると、これが中断されることに注意してください。 – CodesInChaos

6

あなたのクラスのフィールドにWebClientを保存する必要があります。