Reputation: 141
Good day,
I need some help please. I have tried multiple methods to update a progressbar from a backgroundworker and the invoke method with no avail. I have all my code in a module which i want to access from the main ui. I start the backgroundworker from the mainform and call my procedures for the module which needs to report back to the mainforms ui (Progressbar). PBar is a public intreger that counts as the progress count for the Progress bar. BackGroundWorker:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
PastelGRNS(0)
PastelRTS(0)
PastelINVS(0)
PastelCRNS(0)
End Sub
Private Sub BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
Me.PB1.Value = CInt(PBar)
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
Handles BackgroundWorker.RunWorkerCompleted
If e.Cancelled = True Then
Alert(Me, "Canceled", "The process has been canceled.", MessageBoxButtons.OK)
ElseIf e.Error IsNot Nothing Then
Alert(Me, "Error", e.Error.Message, MessageBoxButtons.OK)
Else
Alert(Me, "Done", "All work has been completed.", MessageBoxButtons.OK)
End If
End Sub
The Invoke ProgressBar:
Delegate Sub SetProgress(ByVal pbar1 As Integer) 'Your delegate..
Public Sub ChangePB(pbar1 As Integer)
If PB1.InvokeRequired Then
Dim d As New SetProgress(AddressOf ChangePB)
Me.Invoke(d, New Object() {pbar1})
Else
PB1.Value = pbar1
End If
End Sub
Snipet of code to update Backgroundworker progress:
PBar = ((cnt / ReadCnt) * 100) * 0.25
PBar = Math.Round(PBar, 0)
MainMenu.BackgroundWorker.ReportProgress(CInt(PBar))
my module has for processors which run i need to update my progress bar acording to the percentage ie PBar = ((cnt / ReadCnt) * 100) * 0.25 (count 25% of the full bar)
Upvotes: 0
Views: 954
Reputation: 27322
I'm not sure why this isn't working. The pattern is pretty simple so I suggest you break it down and find out where the issue is.
Avoid using a global variable to hold the percentage, this should just be passed in when you call ReportProgress
.
Here is a fully working sample:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 1 To 100
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(10)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Edit: You should not be directly updating a Progressbar in a Form from a Module. If you are then your design is wrong (you are not adhering to the Single responsibility principle).
You should do something along the lines of raising an Event that reports the progress, the subscriber than then decide what to do with this information = Thumbs up for code re-use.
Upvotes: 3