[RESOLVED] Progress bar not updating when BackgroundWorker ReportProgress from module
Consider the code below for a simple form with a progress bar, a button, and BGW is a BackgroundWorker with Modifiers = Public. This works but if I change blnRunInModule to true it doesn’t update. Yet the PerformStep is being executed as is apparent by the Debug.WriteLine showing the value increasing. What am I doing wrong?
vb.net Code:
Public Class Form1
Private Sub Button1_Click() Handles Button1.Click
BGW.RunWorkerAsync()
End Sub
Private Sub BGW_DoWork() Handles BGW.DoWork
Dim blnRunInModule As Boolean = False
For intCount As Integer = 1 To 10
If blnRunInModule Then
Module1.MySub(intCount)
Else
BGW.ReportProgress(intCount)
End If
System.Threading.Thread.Sleep(1000)
Next
End Sub
Private Sub BGW_ProgressChanged() Handles BGW.ProgressChanged
ProgressBar1.PerformStep()
Debug.WriteLine("Step executed. Value = " & ProgressBar1.Value)
End Sub
End Class
vb.net Code:
Module Module1
Sub MySub(intCount As Integer)
Form1.BGW.ReportProgress(intCount)
End Sub
End Module
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
Crossing threads, I suspect, although I'm a little surprised you're not raising an exception. Nothing good ever comes of accessing external functions or subs from within the background worker. Clearly in this case you're blocking the paint events for the progress bar in so doing. Far less innocuous effects could occur. What happens in the Background Worker stays in the Background Worker!
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
As I understand it, it's because you are updating the wrong instance of Form1 when you use the code in the Module.
I think this thread from last year will explain your problem.
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
Quote:
Originally Posted by
Inferrd
As I understand it, it's because you are updating the wrong instance of Form1 when you use the code in the Module.
I think
this thread from last year will explain your problem.
Don't think that can be true because, as the OP says, the ProgressBar Value is updated. It's just that the paint of the bar doesn't happen.
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
Quote:
Originally Posted by
dunfiddlin
Don't think that can be true because, as the OP says, the ProgressBar Value is updated. It's just that the paint of the bar doesn't happen.
Yes, it's a progress bar on a different instance of Form1, and that instance isn't being shown. Have a read of the thread.
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
Ok, MS weirdness. Not sure I still fully understand it but I guess it makes more sense than my suggestion. Either way, I'll stick to my last stated principle if only for a quiet life!
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
I saw that post of John's and I'm beginning to see the light a little. Also with what dunfiddlin said on my other post. I can see I'm going down the wrong path here so I'll try another tack by creating a class to contain the BackgroundWorker.
Re: Progress bar not updating when BackgroundWorker ReportProgress from module
The problem in the original example is when I execute the Form1.BGW.ReportProgress it’s not getting to the correct Form1. It’s finding the ProgressChanged under the default instance of Form1 which is a copy for the worker thread I guess because it’s being passed ByVal. Since this copy of Form1 is on the worker thread it’s executing it’s ProgressChanged in that form which is on the worker thread. My mistake was adding “Form1.” In front of ReportProgress in the module.
I remembered that passing a parameter can be done ByRef instead of the default. This in fact creates a new variable which simply contains the address of the original object and therefore doesn’t create another instance and the called procedure can modify the original object where it was called from. IE it doesn’t create a copy of the object but rather points back to the original. That was all I needed. Now in the module when I reference BGW.whatever it’s using the original one which gets the correct ProgressChanged executed. Note I now pass the BackgroundWorker to the module by reference and removed the "Form1."
vb.net Code:
Imports System.ComponentModel
Module Module1
Sub MySub(intCount As Integer, ByRef BGW As BackgroundWorker)
BGW.ReportProgress(intCount)
End Sub
End Module
vb.net Code:
Public Class Form1
Private Sub Button1_Click() Handles Button1.Click
ProgressBar1.Value = 0
BGW.RunWorkerAsync()
End Sub
Private Sub BGW_DoWork() Handles BGW.DoWork
For intCount As Integer = 1 To 10
If chkRunInModule.Checked Then
Module1.MySub(intCount, BGW)
Else
BGW.ReportProgress(intCount)
End If
System.Threading.Thread.Sleep(1000)
Next
End Sub
Private Sub BGW_ProgressChanged() Handles BGW.ProgressChanged
ProgressBar1.PerformStep()
End Sub
End Class