cancel backgroundworker  from a separate form

hrodriguez761
hrodriguez761 used Ask the Experts™
on
Hi Experts,
 
i'm very new at VB and here is my dilema, i have form A that calls a backgroundwoeker to run a very long process. once the user clicks on submit on form A it calls the BGworker and opens form B.

 on form B load i'm closing form A - form B has a progress bar, a cancel button and other info i display to the user what i'm stuck is with the cancel button - how can i have form B send a cancelationrequest to the backgroundworker.

 Also if not too much to ask - how can i catch progresschangeevents and workercompleted events in form B

Many thanks in advance.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Where is the backgroundworker? Is it on FormA or on a separate class? You can pass the reference of backgroundworker from FormA to FormB(using a class level variable in FormB) and then use it to cancel. To catch the events, you would need to use AddHandler statement.

Why not add the backgroundworker on FormB?

Author

Commented:
Hi CodeCruiser,

 The BGworker is on a dll i created with the entire long process - i'm passing some data from FORM A to it - that's why im calling  it from there - FORM B has the progress bar and other indicators i want the user to see while the BGWorker is doing it's thing.

Thanks,
Most Valuable Expert 2012
Top Expert 2014
Commented:
Two options then

1) Eliminate formB and show progress etc on FormA
2) Use FormA to pass the reference of BGW to FormB like below

FormB:

Public BGW As BackGroundWorker

BGW_ProgressChanged
BGW_WorkerCompleted


FormA:
Dim frm As New FormB
frm.BGW = dll.BGW
AddHandler dll.BGW.ProgressChanged, AddressOf frm.ProgressChanged
AddHandler dll.BGW_WorkerCompleted, AddressOf frm.WorkerCompleted
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Send the object of BackGroundWorker Class as a parameter to a parameterized constructor of Form B. Then in a button click from Form B. Call the CancelAsync() Method of BackGroundWorker object. Like so Suppose name of the object is bgWorker :
bgWorker.CancelAsync();

Open in new window

Constructor of Form B will be as follows :
Dim BackGroundWorker bgWorker
Public FormB(BackGroundWorker worker)
Begin
Me.bgWorker = worker
End

Open in new window

Author

Commented:
Sorry guys,

i have been trying to get this to work for the past hour with no luck. and I made a mist when i was writing my explanation above.  the backgroundwork is being called from FORM A - and sending all the process contained in the dll to it.  I'm not sure if this changes things.

@codecruiser - I guess i could do option 1 if everything else fails

 @ starlite551 - I did not understood how to implement the constructor on form b  - i have attached my current code on FORM B

Thanks you both
Imports System.ComponentModel

Public Class FProcessing

    Private Sub FProcessing_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FParse.Close()
        Timer1.Interval = 1000
        Timer1.Start()

    End Sub

    Private Sub Timer1_Timer(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label6.Text = myDLL.PUBLICVARIABLES.VAR1        Label3.Visible = Not Label3.Visible
        ProgressBar1.Maximum = 100 + myDLL.PUBLICVARIABLES.tot
        ProgressBar1.Value = 1 + myDLL.PUBLICVARIABLES.cpa
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'If bg.WorkerSupportsCancellation = True Then
        'bw.CancelAsync()
        'End If

    End Sub

Open in new window

Most Valuable Expert 2012
Top Expert 2014

Commented:
>Dim BackGroundWorker bgWorker
>Public FormB(BackGroundWorker worker)

That's a great blend of VB and C# code!
Sorry but I am not much familiar with VB.NET Syntax.. But I could have given better solution in C#
@hrodriguez761: Please post here the code for both the Forms A and B.. I shall convert the code to C#.NET and then solve the problem and convert it back to VB.NET.. As I am not much familiar with VB.NET to be honest.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial