2011-06-26 93 views
3

我想撥打CancelAsync,但webClient超出範圍。幫助功能超出範圍(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

@詹姆斯·巴特勒:歡迎... –

+4

注意,如果用戶點擊了兩次DownloadButton不取消,這打破。 – CodesInChaos

6

你需要在一個領域的WebClient存儲在你的類。