chadmanvb
asked on
update progress bar in the background visual basic .net
I have a progress bar I want to run in the background. I have it display, but it's just blank and does not update. Here is how I have it setup.
first I call it in a sub with
BackgroundWorker1.RunWorke rAsync()
Private Sub BackgroundWorker1_DoWork(B yVal sender As System.Object, ByVal e As System.ComponentModel.DoWo rkEventArg s) Handles BackgroundWorker1.DoWork
Dim intCounter As Integer = 0
ProgressBar1.Maximum = 100
For x = 0 To 100 - 1
System.Threading.Thread.Sl eep(20) 'set delay for progress bar
intCounter = intCounter + 1
BackgroundWorker1.ReportPr ogress(x)
Application.DoEvents() 'force progress bar update
Next
End Sub
Private Sub BackgroundWorker1_Progress Changed(By Val sender As Object, ByVal e As System.ComponentModel.Prog ressChange dEventArgs ) Handles BackgroundWorker1.Progress Changed
' Here's where you update the progressbar
ProgressBar1.Value = e.ProgressPercentage
Application.DoEvents() 'force progress bar update
End Sub
first I call it in a sub with
BackgroundWorker1.RunWorke
Private Sub BackgroundWorker1_DoWork(B
Dim intCounter As Integer = 0
ProgressBar1.Maximum = 100
For x = 0 To 100 - 1
System.Threading.Thread.Sl
intCounter = intCounter + 1
BackgroundWorker1.ReportPr
Application.DoEvents() 'force progress bar update
Next
End Sub
Private Sub BackgroundWorker1_Progress
' Here's where you update the progressbar
ProgressBar1.Value = e.ProgressPercentage
Application.DoEvents() 'force progress bar update
End Sub
ASKER
I still cant get this to work. I do see the progress bar come up on the screen and it goes away in a few seconds, but never updates. Just a white background. Then when I call the sub again, I see the progress bar is full, stays up a few seconds and goes away. Not sure why it's not deplaying the movement.
'refresh sub*********************** *****
Public Sub RefreshStatus()
'start progressbar
ProgressBar1.Visible = True
ProgressBar1.Maximum = 100
BackgroundWorker1.RunWorke rAsync()
'here I do some stuff that takes about 2 seconds
ProgressBar1.Visible = False
exit sub
Private Sub BackgroundWorker1_DoWork(B yVal sender As System.Object, ByVal e As System.ComponentModel.DoWo rkEventArg s) Handles BackgroundWorker1.DoWork
Dim intCounter As Integer = 0
'ProgressBar1.Maximum = 100
For x = 0 To 100 - 1
System.Threading.Thread.Sl eep(20) 'set delay for progress bar
intCounter = intCounter + 1
BackgroundWorker1.ReportPr ogress(x)
Application.DoEvents() 'force progress bar update
Next
End Sub
Private Sub BackgroundWorker1_Progress Changed(By Val sender As Object, ByVal e As System.ComponentModel.Prog ressChange dEventArgs ) Handles BackgroundWorker1.Progress Changed
' Here's where you update the progressbar
ProgressBar1.Value = e.ProgressPercentage
Application.DoEvents() 'force progress bar update
End Sub
'refresh sub***********************
Public Sub RefreshStatus()
'start progressbar
ProgressBar1.Visible = True
ProgressBar1.Maximum = 100
BackgroundWorker1.RunWorke
'here I do some stuff that takes about 2 seconds
ProgressBar1.Visible = False
exit sub
Private Sub BackgroundWorker1_DoWork(B
Dim intCounter As Integer = 0
'ProgressBar1.Maximum = 100
For x = 0 To 100 - 1
System.Threading.Thread.Sl
intCounter = intCounter + 1
BackgroundWorker1.ReportPr
Application.DoEvents() 'force progress bar update
Next
End Sub
Private Sub BackgroundWorker1_Progress
' Here's where you update the progressbar
ProgressBar1.Value = e.ProgressPercentage
Application.DoEvents() 'force progress bar update
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thats sounds like my issue. I'm calling a dll to ssh to a linux server, run some commands, and redirect the output to the user on a listview and label. I'm not sure how to run the other items in the background and then display them on my main form. Maybe run the progress bar on an other form or this not a good idea?
Well...the simplistic answer is to run those commands in the DoWork() handler of a BackgroundWorker()! Use either the ProgressChanged() or RunWorkerCompleted() events to pass the info back to the UI.
have you set the WorkerReportsProgress property to True?
check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
ASKER
Thanks, that's what I was doing.
This line should cause an error, since you are setting a UI control value directly from the background thread:
ProgressBar1.Maximum = 100
You need to invoke that, or set from the main UI thread before starting the background thread.
The Application.DoEvents() lines should not be necessary.
Other than that, I don't see anything wrong with the code.
With that in mind, can you show the complete forms code?...