2013-07-07 88 views
0

我試圖讓進度條顯示當前進度,但由於某種原因進度條只顯示在那裏,沒有任何事情發生。我使用下面的代碼Progressbar沒有被backgroundworker更新

Private Sub BuildApp_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BuildApp.ProgressChanged 
     Me.ProgressBar1.Value = e.ProgressPercentage 
End Sub 

我檢查後臺工作人員的屬性是否設置爲報告狀態,並將其設置爲「是」。另外,我檢查了進度條的樣式,它被設置爲Blocks。任何幫助?

+0

從DoWork的內()處理程序需要調用ReportProgress(),並傳遞當前百分比值。你是這麼做的?... –

+0

因此,這意味着我需要在bg工人的每個操作後手動鍵入一個值? –

+0

@KrešimirČulina是 - 背景工作者無法神奇地確定剩餘工作量! – Blorgbeard

回答

4

這裏有一個簡單的例子...

Public Class Form1 

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
     Button1.Enabled = False 
     BuildApp.RunWorkerAsync() 
    End Sub 

    Private Sub BuildApp_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BuildApp.DoWork 
     Dim someTotal As Integer = 12 
     For x As Integer = 1 To someTotal 
      System.Threading.Thread.Sleep(1000) ' simulated work 
      BuildApp.ReportProgress(x/someTotal * 100) ' pass the current percentage value 
     Next 
    End Sub 

    Private Sub BuildApp_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BuildApp.ProgressChanged 
     Me.ProgressBar1.Value = e.ProgressPercentage 
    End Sub 

    Private Sub BuildApp_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BuildApp.RunWorkerCompleted 
     MessageBox.Show("Done!") 
     Button1.Enabled = True 
    End Sub 

End Class 
+0

如果你沒有一個像上面的例子那樣很好的循環,那麼你只需手動將ReportProgress()調用放在適當位置的代碼中並使用適當的值。 –

+0

或者只是使用動畫gif,如果它太難估計總時間。 http://stackoverflow.com/questions/13206926/vb-net-progressbar-backgroundworker/13486676#13486676 –

+1

如果你沒有一個很好的「適合」具體的進展,那麼只需將ProgressBar樣式()屬性設置爲選取框。與動畫GIF相同的東西... –