Solved

Background worker cancelling from another form

Posted on 2016-09-23
8
22 Views
Last Modified: 2016-10-09
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

0
Comment
Question by:K-9
  • 4
  • 2
8 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41812492
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
 

Author Comment

by:K-9
ID: 41812493
Yes I did: Private myParent As frmGMapNet
frmGMapNew is the form where the Backgroundworker is.
0
 

Author Comment

by:K-9
ID: 41812502
Wow.. that's weird.  If I use frmGMapNew.BackgroundWorker1.CancelAsync() it works.  For some reason it doesn't like using myParent
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41812534
Sounds like you have the BackgroundWorker1 as a static variable in that case.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41812537
ps.  I told you why the myParent doesn't work in my first comment.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41828034
My first comment points out what is wrong with the code supplied and how to fix it.
#a41812492
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question