2013-02-24 98 views
0

我有2個進度條。現在我在代碼中做了一些其他的事情,這需要時間來執行,因此需要使用backgroundworker。我沒有太多的想法如何使用backgroundworker。我這裏沒有包含的其他代碼執行得很好,但progressbar的值不會改變,也不會改變它們的文本。我將如何實現這一目標?VB.NET從BackgroundWorker更改進度條值?

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

    Dim LM As RegistryKey = Registry.LocalMachine 
    Dim LM_SW As RegistryKey = LM.OpenSubKey("Software") 
    Dim LM_MS As RegistryKey = LM_SW.OpenSubKey("Microsoft") 
    Dim LM_Win As RegistryKey = LM_MS.OpenSubKey("Windows") 
    Dim LM_CV As RegistryKey = LM_Win.OpenSubKey("CurrentVersion") 
    Dim AppPaths As RegistryKey = LM_CV.OpenSubKey("App Paths") 
    Dim NrOfFiles1 As Integer = AppPaths.SubKeyCount 

    ProgressBar2.Maximum = NrOfFiles1 
    ProgressBar1.Maximum = 100 

    For Each FormatString As String In AppPaths.GetSubKeyNames() 
     ProgressBar2.Value += 1/NrOfFiles1 
     ProgressBar1.Value += 1 * ProgressBar2.Value/100/10 
     ProgressBar1.Text = ProgressBar1.Value & "%" 
     ProgressBar2.Text = ProgressBar2.Value & "%" 
    Next 

    ProgressBar2.Value = 0 

End Sub 
+0

是的,這樣做的工作,比我想象的要簡單得多:)謝謝 – Legjendat 2013-02-26 03:07:03

回答

2

使用Backgroundworker的ProgressChanged事件。 在你的DoWork方法,調用

BackgroundWorker1.ReportProgress(Percentage) 

凡百分比是一個整數值

然後在ProgressChanged事件,你可以操縱你的進度條。

Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork 

    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker) 

    '''YOUR OTHER CODE 
    worker.ReportProgress(PERCENTAGE) 
    '''YOUR OTHER CODE 

    End Sub 

    ' This event handler updates the progress. 
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object,   ByVal e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged 
     ProgressBar1.Text = e.ProgressPercentage.ToString() & "%" 
     ProgressBar1.Value = e.ProgressPercentage 
    End Sub