下面的代碼無法正常工作,不知何故,我可以從「已完成」方法的int值不了我的btn_Start_Click方法:打開文件完成
private void btn_Start_Click(object sender, EventArgs e)
{
int completedDownload = 0;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://somesite.com/file.jpg"), @"c:\file.jpg");
if (Completed.completeDownload == 1)
{
//open the file code goes here.
}
//Rest of the code goes here.
//and here
//and here
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
completedDownload = 1;
}
「異步」 的意思是 「以後發生」。將DownloadFileAsync調用後的所有代碼移至Completed方法。請注意,Completed由線程池線程調用,您需要使用Control.BeginInvoke()來運行更新UI的任何代碼。 – 2012-03-10 14:59:30