2013-02-07 30 views
0

我知道這裏有很多問題,但我經歷了很多問題,並且運氣不大。我對事件和後臺工作人員很陌生,我只是不知道實現這一點的最佳方式。從下載類中更新表單中的進度條

我在C#中有一個基本的Windows窗體。它包含一個進度條。我正在調用一個下載文件的類。我希望進度條根據下載進行更新。如果一切都在同一班級,我的工作狀況良好,但在這種情況下我無法實現。處理這個問題的最佳方法是什麼?我該如何處理?目前,我這樣做:

WebClient downloader = new WebClient();    
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 

然後進步改變了我這樣做:

public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    pbDownload.Value = e.ProgressPercentage; 
} 

但是,當我把所有這一切都只是在一個單獨的類中的進度條,它得到所有弄亂。想法?謝謝!

+0

以何種方式搞砸?錯誤,沒有反應好,...? – Jester

回答

0

我有這個魔女大約是一樣的你正在嘗試做的,工作正常 FStatus ......形式參考

FrmStatus FStatus = null; 

     public void ProgressInit(String caption, string opis, int Max) 
     { 
      FStatus = new SZOKZZ.FrmStatus(); 
      FStatus.InitProc(Max); 
      FStatus.SetCaption(caption, opis); 
      FStatus.Show(); 
     } 

     public void DLProgress(string curl, string cdl) 
     { 
      WebClient webClient = new WebClient(); 
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DLDone); 
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DLSetProc); 
      webClient.DownloadFileAsync(new Uri(curl), cdl); 
     } 
     private void DLSetProc(object sender, DownloadProgressChangedEventArgs e) 
     { 
      this._FileProcentDownloaded = e.ProgressPercentage; 
      FStatus.SetProcDL(this._FileProcentDownloaded); 

     } 
     private void DLDone(object sender, AsyncCompletedEventArgs e) 
     { 
      _DlError = e.Error; 
      FStatus.Dispose(); 
      FStatus = null; 
     } 
0

,你應該調用Application.DoEvents();根據他們對新價值觀來強制的形式更新控件:

public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
     { 
      pbDownload.Value = e.ProgressPercentage; 
      Application.DoEvents(); 
     } 

問候