2012-12-02 36 views
0

我做了什麼@Enigmativity寫 那就是:Web客戶端不支持併發I/O操作

 Action<int, ProgressBar, Label, Label, int, Button> downloadFileAsync = (i, pb, label2, label1, ServID, button1) => 
    { 
     var bd = AppDomain.CurrentDomain.BaseDirectory; 
     var fn = bd + "/" + i + ".7z"; 
     var down = new WebClient(); 
     DownloadProgressChangedEventHandler dpc = (s, e) => 
     { 
      label1.Text = "Download Update: " + i + " From: " + ServID; 
      int rec =Convert.ToInt16(e.BytesReceived/1024); 
      int total =Convert.ToInt16(e.TotalBytesToReceive/1024) ; 
      label2.Text = "Downloaded: " + rec.ToString() + "/" + total.ToString() + " KB"; 
      pb.Value = e.ProgressPercentage; 
     }; 
     AsyncCompletedEventHandler dfc = null; dfc = (s, e) => 
     { 
      down.DownloadProgressChanged -= dpc; 
      down.DownloadFileCompleted -= dfc; 
      CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory); 
      File.Delete(fn); 
       if (i == ServID) 
       { 

        button1.Enabled = true; 
        label1.Text = "Game Is Up-To-Date.Have Fun!!"; 
        label2.Text = "Done.."; 
       } 
     down.Dispose(); 
     }; 

我唯一problam現在當程序extarting下載的文件

CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory); 

在某些文件中需要花費時間來下載下載的文件 ,所以我如何告訴程序等待解壓縮完成?

+6

請改善您的代碼縮進和行格式,現在它是不可讀的。也使用完整的單詞,而不僅僅是因爲它很煩人。 –

+0

+1 - 無法讀取 – SalientBrain

回答

2

嘗試定義一個lambda來封裝單個異步下載,然後在循環中調用它。

這裏的拉姆達:

Action<int> downloadFileAsync = i => 
{ 
    var bd = AppDomain.CurrentDomain.BaseDirectory; 
    var fn = bd + "/" + i + ".7z"; 
    var wc = new WebClient(); 
    DownloadProgressChangedEventHandler dpc = (s, e) => 
    { 
     progressBar1.Value = e.ProgressPercentage; 
    }; 
    AsyncCompletedEventHandler dfc = null; 
    dfc = (s, e) => 
    { 
     wc.DownloadProgressChanged -= dpc; 
     wc.DownloadFileCompleted -= dfc; 
     CompressionEngine.Current.Decoder.DecodeIntoDirectory(fn, bd); 
     File.Delete(fn); 
     wc.Dispose(); 
    }; 
    wc.DownloadProgressChanged += dpc; 
    wc.DownloadFileCompleted += dfc; 
    wc.DownloadFileAsync(new Uri(Dlpath + i + "/" + i + ".7z"), fn); 
}; 

你會注意到,它很好地分離的所有事件,並正確WebClient實例的部署。

現在這樣稱呼它:

while (i <= ServID) 
{ 
    downloadFileAsync(i); 
    i++; 
} 

你有進度條更新搗鼓正確顯示的所有下載文件的進展,但在原則上這應該爲你工作。

+0

謝謝你,你非常幫助我 –