Reputation: 3
I have some code I'm trying to run while updating a progress bar but instead it runs my code, then the bar fills. I'm using background worker but I'm guessing I'm doing something wrong. I have Visual Studio 2019. It's telling me it needs more details to post this question, but there isn't much else to post.
txtBrantTrans.Text = Get_Transaction_Number("Brantford")
txtCambTrans.Text = Get_Transaction_Number("Cambridge")
txtParisTrans.Text = Get_Transaction_Number("Paris")
txtWoodTrans.Text = Get_Transaction_Number("Woodstock")
txtSite.Text = Format(Get_Last_Transaction_DateTime(" "), "MMM dd, yyyy HH:mm")
txtSite00.Text = Format(Get_Last_Transaction_DateTime("00"), "MMM dd, yyyy HH:mm")
txtSite01.Text = Format(Get_Last_Transaction_DateTime("01"), "MMM dd, yyyy HH:mm")
txtSite02.Text = Format(Get_Last_Transaction_DateTime("02"), "MMM dd, yyyy HH:mm")
txtSite03.Text = Format(Get_Last_Transaction_DateTime("03"), "MMM dd, yyyy HH:mm")
txtSite04.Text = Format(Get_Last_Transaction_DateTime("04"), "MMM dd, yyyy HH:mm")
txtSite05.Text = Format(Get_Last_Transaction_DateTime("05"), "MMM dd, yyyy HH:mm")
txtSite06.Text = Format(Get_Last_Transaction_DateTime("06"), "MMM dd, yyyy HH:mm")
txtSite07.Text = Format(Get_Last_Transaction_DateTime("07"), "MMM dd, yyyy HH:mm")
txtSite08.Text = Format(Get_Last_Transaction_DateTime("08"), "MMM dd, yyyy HH:mm")
txtSite09.Text = Format(Get_Last_Transaction_DateTime("09"), "MMM dd, yyyy HH:mm")
txtSite10.Text = Format(Get_Last_Transaction_DateTime("10"), "MMM dd, yyyy HH:mm")
txtSite12.Text = Format(Get_Last_Transaction_DateTime("12"), "MMM dd, yyyy HH:mm")
txtSite14.Text = Format(Get_Last_Transaction_DateTime("14"), "MMM dd, yyyy HH:mm")
txtSite15.Text = Format(Get_Last_Transaction_DateTime("15"), "MMM dd, yyyy HH:mm")
txtSite16.Text = Format(Get_Last_Transaction_DateTime("16"), "MMM dd, yyyy HH:mm")
txtSite17.Text = Format(Get_Last_Transaction_DateTime("17"), "MMM dd, yyyy HH:mm")
Change_Colour(txtSite)
Change_Colour(txtSite00)
Change_Colour(txtSite01)
Change_Colour(txtSite02)
Change_Colour(txtSite03)
Change_Colour(txtSite04)
Change_Colour(txtSite05)
Change_Colour(txtSite06)
Change_Colour(txtSite07)
Change_Colour(txtSite08)
Change_Colour(txtSite09)
Change_Colour(txtSite10)
Change_Colour(txtSite12)
Change_Colour(txtSite14)
Change_Colour(txtSite15)
Change_Colour(txtSite16)
Change_Colour(txtSite17)
End Sub
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
'Fill_Form()
bgwFillInfo.WorkerReportsProgress = True
bgwFillInfo.RunWorkerAsync()
End Sub
Private Sub bgwFillInfo_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgwFillInfo.DoWork
Me.Invoke(Sub() Fill_Form())
For i As Integer = 0 To 10000
If i Mod 1000 Then
bgwFillInfo.ReportProgress(i / 100)
End If
Next
End Sub
Private Sub bgwFillInfo_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgwFillInfo.ProgressChanged
pbFillInfo.Value = e.ProgressPercentage
End Sub```
Upvotes: 0
Views: 1053
Reputation: 3
With JM's help I was able to get it working amazingly. Here is my code for anyone who wants an example
My refresh button does this but you could also do this on form load
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
bgwFillInfo.WorkerReportsProgress = True
bgwFillInfo.RunWorkerAsync()
End Sub
Then the DoWork of the BackGroundWork is this
Private Sub bgwFillInfo_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgwFillInfo.DoWork
Dim textBoxes() As TextBox = {txtBrantTrans, txtCambTrans, txtParisTrans, txtWoodTrans, txtSite, txtSite00, txtSite01, txtSite02, txtSite03, txtSite04, txtSite05, txtSite06, txtSite07, txtSite08, txtSite09, txtSite10, txtSite12, txtSite14, txtSite15, txtSite16, txtSite17}
Dim locname As String = ""
Dim data As String = ""
For i = 0 To textBoxes.GetUpperBound(0)
Dim progress = (i + 1) * 100 \ textBoxes.Length
Select Case textBoxes(i).Name
Case "txtBrantTrans"
data = Get_Transaction_Number("Brantford").ToString
Case "txtCambTrans"
data = Get_Transaction_Number("Cambridge").ToString
Case "txtParisTrans"
data = Get_Transaction_Number("Paris").ToString
Case "txtWoodTrans"
data = Get_Transaction_Number("Woodstock").ToString
Case "txtSite"
data = Format(Get_Last_Transaction_DateTime(" "), "MMM dd, yyyy HH:mm").ToString
Case "txtSite00"
data = Format(Get_Last_Transaction_DateTime("00"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite01"
data = Format(Get_Last_Transaction_DateTime("01"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite02"
data = Format(Get_Last_Transaction_DateTime("02"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite03"
data = Format(Get_Last_Transaction_DateTime("03"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite04"
data = Format(Get_Last_Transaction_DateTime("04"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite05"
data = Format(Get_Last_Transaction_DateTime("05"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite06"
data = Format(Get_Last_Transaction_DateTime("06"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite07"
data = Format(Get_Last_Transaction_DateTime("07"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite08"
data = Format(Get_Last_Transaction_DateTime("08"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite09"
data = Format(Get_Last_Transaction_DateTime("09"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite10"
data = Format(Get_Last_Transaction_DateTime("10"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite12"
data = Format(Get_Last_Transaction_DateTime("12"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite14"
data = Format(Get_Last_Transaction_DateTime("14"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite15"
data = Format(Get_Last_Transaction_DateTime("15"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite16"
data = Format(Get_Last_Transaction_DateTime("16"), "MMM dd, yyyy HH:mm").ToString
Case "txtSite17"
data = Format(Get_Last_Transaction_DateTime("17"), "MMM dd, yyyy HH:mm").ToString
End Select
Dim packet = Tuple.Create(textBoxes(i), data)
bgwFillInfo.ReportProgress(progress, packet)
Next
End Sub
The ProgressChanged of the BackGroundWork looks like this
Private Sub bgwFillInfo_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgwFillInfo.ProgressChanged
Dim packet = DirectCast(e.UserState, Tuple(Of TextBox, String))
Dim textBox = packet.Item1
Dim data = packet.Item2
textBox.Text = data
Change_Colour(textBox)
pbFillInfo.Value = e.ProgressPercentage
End Sub
Upvotes: 0
Reputation: 54417
Here's an example of how you might update the UI in the ProgressChanged
event handler while doing the rest of the work in the DoWork
event handler.
DoWork
:
Dim textBoxes = {TextBox1, TextBox2, ..., TextBoxN}
For i = 0 To textBoxes.GetUpperBound(0)
Dim progress = (i + 1) * 100 \ textBoxes.Length
Dim data = GetData()
Dim packet = Tuple.Create(textBoxes(i), data)
BackgroundWorker1.ReportProgress(progress, packet)
Next
ProgressChanged
:
Dim packet = DirectCast(e.UserState, Tuple(Of TextBox, String))
Dim textBox = packet.Item1
Dim data = packet.Item2
textBox.Text = data
ProgressBar1.Value = e.ProgressPercentage
You do the work of getting the data on the background thread and then you pass the data, the control to update with that data and the current progress, then you update that control with the data and the ProgressBar
on the UI thread. You can package up the control and the data in whatever way you deem appropriate. I've used a Tuple
here but you might choose to use an array or your own dedicated type.
Upvotes: 2