Solved

Background worker cancelling from another form

Posted on 2016-09-23
8
14 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now