Avatar of hrodriguez761
hrodriguez761

asked on 

cancel backgroundworker from a separate form

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.
Visual Basic.NET.NET ProgrammingVisual Basic Classic

Avatar of undefined
Last Comment
starlite551
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of hrodriguez761
hrodriguez761

ASKER

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,
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of starlite551
starlite551
Flag of India image

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

Avatar of starlite551
starlite551
Flag of India image

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

Open in new window

Avatar of hrodriguez761
hrodriguez761

ASKER

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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

>Dim BackGroundWorker bgWorker
>Public FormB(BackGroundWorker worker)

That's a great blend of VB and C# code!
Avatar of starlite551
starlite551
Flag of India image

Sorry but I am not much familiar with VB.NET Syntax.. But I could have given better solution in C#
Avatar of starlite551
starlite551
Flag of India image

@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.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo