2012-12-15 40 views
0

這是我的代碼:一直等到下載結束

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 + "/" + ServID; 
      int rec =Convert.ToInt16(e.BytesReceived/1024); 
      int total =Convert.ToInt16(e.TotalBytesToReceive/1024) ; 
      label2.Text = "Downloaded: " + rec.ToString() + "KB/" + total.ToString() + " KB"; 
      pb.Value = e.ProgressPercentage; 
     }; 
     AsyncCompletedEventHandler dfc = null; dfc = (s, e) => 
     { 
      label1.Text = "Extracting Files..."; 
      Rar Ra = new Rar(); 
      Ra.Open(i + ".7z"); 
      Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 
      File.Delete(fn); 
      down.DownloadProgressChanged -= dpc; 
      down.DownloadFileCompleted -= dfc; 
      down.Dispose(); 
      if (ServID == i) 
      { 
       button1.Enabled = true; 
       label1.Text = "Have Fun In-Game..."; 
       label2.Text = "Done..."; 
      } 
     }; 
     down.DownloadProgressChanged += dpc; 
     down.DownloadFileCompleted += dfc; 
     down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn); 
    }; 

這是我的電話:

   while (i <= ServID) 
       { 
        downloadFileAsync(i, progressBar1, label2, label1, ServID, button1); 
        i++; 
       } 

解壓:

public void Decompress(int i) 
    { 

     Rar Ra = new Rar(); 
     Ra.Open(i + ".7z"); 
     Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 

    } 

在此代碼程序下載所有更新的一次... 它開始分解下載完成的第一個。 我需要應用程序一次只下載一個更新,然後對其進行分析。

+0

你在做什麼麻煩?你試圖做這個你自己有什麼問題? –

回答

0

您可以使用unelegant解決方案使用布爾值:

你應該頂層拉姆達進入新async方法。你可以在正常情況下調用它。之後,在頂層lambda的開始處聲明布爾型downloadCompleted並將其設置爲false。然後轉到AsyncCompletedEventHandler,最後將downloadCompleted的值設置爲true - 這將標誌我們完成的最後一個while循環。頂級lamba的結束應該等到完成下載和壓縮。這不會將應用程序的狀態更改爲「無響應」。

電話:

downloadFileAsync = (i, pb, label2, label1, ServID, button1) => 
    { 
     Download(i, pb, label2, label1, ServID, button1); 
    }; 

的新方法:

public async Task<int> Download(...) 
    { 
     bool downloadCompleted = false; 
     var bd = AppDomain.CurrentDomain.BaseDirectory; 
     var fn = bd + "/" + i + ".7z"; 
     var down = new WebClient(); 
     DownloadProgressChangedEventHandler dpc = (s, e) => 
     { 
      label1.Text = "Download Update: " + i + "/" + ServID; 
      int rec = Convert.ToInt16(e.BytesReceived/1024); 
      int total = Convert.ToInt16(e.TotalBytesToReceive/1024); 
      label2.Text = "Downloaded: " + rec.ToString() + "KB/" + total.ToString() + " KB"; 
      pb.Value = e.ProgressPercentage; 
     }; 
     AsyncCompletedEventHandler dfc = null; dfc = (s, e) => 
     { 
      label1.Text = "Extracting Files..."; 
      Rar Ra = new Rar(); 
      Ra.Open(i + ".7z"); 
      Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 
      File.Delete(fn); 
      down.DownloadProgressChanged -= dpc; 
      down.DownloadFileCompleted -= dfc; 
      down.Dispose(); 
      if (ServID == i) 
      { 
       button1.Enabled = true; 
       label1.Text = "Have Fun In-Game..."; 
       label2.Text = "Done..."; 
      } 
      downloadCompleted = true; 
     }; 
     down.DownloadProgressChanged += dpc; 
     down.DownloadFileCompleted += dfc; 
     down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn); 
     while (!downloadCompleted) 
      ; 
    } 

調用while循環可以保持原樣。

+0

我需要c#5公共異步? –

+0

tnx man我mannge得到這個工作.. –