Question Background Worker

jdh8865

New member
Joined
Nov 22, 2009
Messages
3
Programming Experience
5-10
Hello, I am fairly new to VB.net and am experimenting with the BackGroundWorker.
I have the following code in a simple form and also some code in a standard module:

VB.NET:
Public Class Form1

Public WithEvents TestWorker As BackgroundWorker

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

btnStart.Enabled = False
btnCancel.Enabled = True
Me.ProgressBar1.Value = 0
TestWorker = New BackgroundWorker
TestWorker.WorkerReportsProgress = True
TestWorker.WorkerSupportsCancellation = True
TestWorker.RunWorkerAsync()

End Sub

Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles TestWorker.DoWork

DoCount()

End Sub

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

Me.ProgressBar1.Value = e.ProgressPercentage

End Sub

Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

TestWorker.CancelAsync()

End Sub

Private Sub TestWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles TestWorker.RunWorkerCompleted

btnStart.Enabled = True
btnCancel.Enabled = False

End Sub
End Class


Module Module1


Public Sub DoCount()

Dim ListText As String
For Value As Integer = 0 To 100

If Form1.TestWorker.CancellationPending Then
Exit For
End If

ListText = String.Concat("Item #", Value)
Form1.TestWorker.ReportProgress(Value, ListText)
Threading.Thread.Sleep(100)

Next

End Sub


End Module
VB.NET:

When I run the program I get an error stating the following: "NullReferenceException was unhandled by user code"

I guess my question is if I start a backgroundworker from a form and then have the dowork event call a sub that resides in a seperate module, how do I get the code in the module to report progress? I hope my explanation made sense.


Thanks for any help.
 
The problem is that module members are Shared, and you refer to a default form instance (Form1) from it, but the default form feature is thread specific so you get a new form instance that has no TestWorker instance. This can be solved by placing the DoCount method in the Form1 class, or place its code in DoWork event handler where you then refer to Me.TestWorker.
 
Hi JohnH,
Thanks for taking a look. With my example in mind, suppose I had two forms on which I have declared a public variable as a backgroundworker. In the DoWork method of the backgroundworker on each form I tell it to run somea procedure that is stored in a standard module. Now I want the code in the standard module to update a progress bar on either form. Is there a way to do that?


Thanks
 
To run a Shared method that is instance dependent you have to pass the instance as input parameter to the method. That the method is instance dependent is usually a good indicator that it should be a instance member of the class, and not a Shared method. This don't have to be a form class, it can be a worker class that you write independent of UI and that raise progress events that any UI instance can use, by doing this the instance dependency is removed.
 
Back
Top