Solved

Background worker cancelling from another form

Posted on 2016-09-23
8
30 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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