Background worker cancelling from another form

Using vb.net for an application i'm developing.  During a backgroundworker routine, I have a semi-transparent overlay form over top with a progress bar and cancel button.  However when I try to cancel I get the error: Object reference not set to an instance of an object.

How can I cancel a backgroundworker from another form?  This is my code from the second (overlay) form.

Public Class CISMapOverlay
     Private myParent As frmGMapNet

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadLabel2.Click
        Me.myParent.BackgroundWorker1.CancelAsync()
        Me.Hide()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Me.myParent = myParent

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class

Open in new window

K-9Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
You never assign a value to your variable myParent in the code you show.  Once you set that to the 'parent' form you should be able to do what you want (BackgroundWorker1 would need to be a public variable).
0
 
K-9Author Commented:
Yes I did: Private myParent As frmGMapNet
frmGMapNew is the form where the Backgroundworker is.
0
 
K-9Author Commented:
Wow.. that's weird.  If I use frmGMapNew.BackgroundWorker1.CancelAsync() it works.  For some reason it doesn't like using myParent
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AndyAinscowFreelance programmer / ConsultantCommented:
Sounds like you have the BackgroundWorker1 as a static variable in that case.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  I told you why the myParent doesn't work in my first comment.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
My first comment points out what is wrong with the code supplied and how to fix it.
#a41812492
0
All Courses

From novice to tech pro — start learning today.