I spent over 25 hours on this and havnt reached my goal. Please suggest.
I have a button (btnUpdate) on a form where I am calling a function. The function updates local sql server table using web services. But the progress bar runs the code (or function) 100 times. Attached text file is the function. All the background and progress stuff codes are shown here.
Private Sub btnSyncNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSyncNew.Click
Me.BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
For i As Integer = 1 To 100
'Raise the ProgressChanged event in the UI thread.
worker.ReportProgress(i, i & " iterations complete")
'Perform some time-consuming operation here.
Dim cSessionID = Me.txtSessionID.Text
Dim objMySlip As New clsSyncSlip()
Dim sResult As Integer
sResult = objMySlip.syncSlipNew(cSessionID)
'MsgBox("Total " & sResult & " slip(s) have been updated")
Threading.Thread.Sleep(0)
Next i
MsgBox("done")
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
Me.Label1.Text = TryCast(e.UserState, String)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Label1.Text = "Operation complete"
End Sub
It takes about a second per record when I run the code without the
progress bar. lets say I have 20 records that code finds to update. I
takes about 15~ seconds to do the job.
with progress bar
For the same task it will run the code and first time I will see that
20 records have been updated but only 1 bar moves to right. Then it
runs the code again and again for many times and i can see the
progress bar moving towards right.
I read fourm somewhere and it's about progress bar without knowing how
long the process will last. But I am not sure how it will work. I get
the part where I have set the property of progress bar min and max (or
is it someplace in the code). LOST...
First, you need to set initial values, declare a counter and setup the
timer :
Bookmarks