Backgroundworker does not report progress

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have the following code:

VB.NET:
Private Sub BackgroundWorkerUpdateListview_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerUpdateListview.DoWork

While TimeUntilUpdate > 0
Threading.Thread.Sleep(500)
TimeUntilUpdate -=1

If forceUpdate = True Then
Exit While
End If
End While

forceUpdate = False

' Do some work

BackgroundWorkerUpdateListview.ReportProgress(100 * rnd)



TimeUntilUpdate = 20

Loop
End Sub

Private Sub bgrLongProcess_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorkerUpdateListview.ProgressChanged

' Report back to GUI

Me.Text = e.ProgressPercentage

End Sub


Basically what is going on is that every 20 seconds this backgroundworker wakes up, does some work, and reports back to GUI
If I let it be (forceUpdate stays false) it reports back as it should. But if I run some code a bit like this:

VB.NET:
Public Sub ButtonClick (...) Handles MyButton.Click
ForceUpdate = True
End Sub

Then it stops calling the ProgressChanged sub (debug shows that it runs the line with ReportProgress in DoWork sub, but it doesn't seem to go into the code)


Anybody who has any idea what could be wrong here?
 
Last edited:
After doing some more tests I have still not found the exact problem, but I've probably gotten closer to the solution:

Private Shared forceUpdate As Boolean = False

Setting forceUpdate = True in direct controls events (e.g. button click, checkbox click, combobox indexchanged, etc) works fine
But in this case it does not work:
VB.NET:
Public Sub DoSomeWork()
forceUpdate = true
End Sub

Private Sub lViewDisp_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lViewDisp.KeyDown
DoSomeWork()
End Sub

DoSomeWork fires correctly and forceUpdate is set to true. The DoWork event gets into the code and according to debugging it tries to fire the ReportProgress event and successfully executes the code just after the ReportProgress line in DoWork. But the ReportProgress event never fires
No exceptions is fired or anything, it just totally ignores that ReportProgress code
 
Back
Top