2015-03-03 54 views
-2

我不想使用downloadfileAsync事件我使用的下載文件任何完成的事件是有的,只有下載文件,任何想法或任何其他方式告訴我,謝謝提前。webclient下載文件,但沒有執行完成

if (!item.IsInstalled && item.IsChecked) 
        { 
         string filename = Path.Combine(Path.GetTempPath(), "AlisAppTemp", "Silverlight"); 
         using (StreamWriter sw = new StreamWriter(filename)) 
         { 
          sw.WriteLine("Error"); 
         } 

         client.DownloadProgressChanged += client_DownloadProgressChanged; 
         client.DownloadFileCompleted += client_DownloadFileCompleted; 
         client.DownloadFile(new Uri("http://download.microsoft.com/download/F/8/C/F8C0EACB-92D0-4722-9B18-965DD2A681E9/30514.00/Silverlight_x64.exe"), filename); 
         item.IsDownloaded = true; 
        } 


private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      double byteIn = double.Parse(e.BytesReceived.ToString()); 
      double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); 
      double percentage = byteIn/totalBytes * 100; 
      DownloadData = "Download " + e.BytesReceived/1024 + " Of " + e.TotalBytesToReceive/1024; 
      this.CurrentProgress = e.ProgressPercentage; 
     } 

     private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
     { 
      if (e.UserState != null && e.UserState is AlisApplicationModel) 
      { 
       AlisApplicationModel ObjApp = e.UserState as AlisApplicationModel; 
       ObjApp.IsDownloaded = true; 
      } 
      if (e.Cancelled) 
      { 
       this.Cleanup(); 

      } 
      //if (!_bw.IsBusy) 
      //{ 
      // _bw.RunWorkerAsync(); 
      //} 

     } 
+0

@whatever什麼原因你下來投票只是給理由,然後downvote – 2015-03-03 08:43:26

回答

0

DownloadFileCompletedDownloadProgressChanged事件將用於異步下載籌集

您正在使用同步DownloadFile方法,因此您的client_DownloadProgressChangedclient_DownloadFileCompleted方法將永遠不會被調用。

所以你必須使用DownloadFileAsync而不是DownloadFile方法。

+0

所以如果我使用下載文件同步,所以我不能更新我的進度條?它使用downloadfileAsync事件complesory? – 2015-03-03 07:21:20

+0

是的。如果你正在使用'DownloadFile'(注意這是方法,而不是你稱之爲的事件) - 那麼'DownloadProgressChanged'和'DownloadFileCompleted'事件不會被引發。如果你想處理這些事件,你可以**使用'DownloadFileAsync'。 – 2015-03-03 07:25:03

+0

@ Dhru'soni順便說一句 - 有沒有什麼理由不喜歡'DownloadFileAsync'的使用,而更喜歡'DownloadFile'? – 2015-03-03 07:26:26