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:
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
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.
I have the following code in a simple form and also some code in a standard module:
VB.NET:
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.