Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.Net      BackgroundWorker

Posted on 2013-01-04
4
Medium Priority
?
483 Views
Last Modified: 2013-01-04
Hi Experts.  I'm learning to use the backgroundworker and have completed the module below.   Clicking BtnStart starts a backgroundworker which updates a progress bar and when completed enters "Process Completed" in a textbox.  So far everything seems to work correctly

Please would someone be kind enough to check my code to see if I have it right but also I dont seem to be able to get the thread to cancel on a command button,  I have tried entering  a line in the do_work sub that checkes if cancel is pending and then using a sub with BGWorker.CancelAsync().  Howver this does not work.

Also, when the thread is running, should I be able to see an additional process in the task manager/Processes?     MAny Thanks



Imports System.Threading
Module TestingBGWorker
    Public WithEvents BGWorker As System.ComponentModel.BackgroundWorker
    Delegate Sub UpdateTextBoxDelegate()
    Dim abc As Integer = 0
    Public Sub StartBGWorker()
        BGWorker = New System.ComponentModel.BackgroundWorker
        BGWorker.WorkerReportsProgress = True
        BGWorker.WorkerSupportsCancellation = True
        BGWorker.RunWorkerAsync()
    End Sub
    Private Sub TestWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWorker.DoWork
        For i = 1 To 100
            Thread.Sleep(50)
            abc = i
            BGWorker.ReportProgress(Nothing)
        Next
    End Sub
    Private Sub TestWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BGWorker.ProgressChanged
        Form1.ProgressBar1.Visible = True
        Form1.ProgressBar1.Value = abc
    End Sub
    Private Sub TestWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWorker.RunWorkerCompleted
        Form1.TextBox1.Text = "Process Complete"
        Form1.ProgressBar1.Visible = False
        Form1.btnCancel.Enabled = False
        Form1.BtnStart.Enabled = True
    End Sub
    Public Sub TestWorker_CancelAsync()
        Form1.btnCancel.Enabled = False
        Form1.BtnStart.Enabled = True
    End Sub
End Module
0
Comment
Question by:PNRT
[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
  • 2
4 Comments
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 800 total points
ID: 38744634
in your for...loop, you need to check for CancellationPending

check http://emoreau.com/Entries/Articles/2006/12/The-BackgroundWorker-component.aspx
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38744875
Also, as shown in Eric's article, pass the progress value out through ReportProgress() instead of using a global variable.

Speaking of global variables, why did you decide to put this code into a Module?  Since you're making everything go back to Form1 through default instance, why not have the code in the Form itself?...
0
 
LVL 2

Author Comment

by:PNRT
ID: 38745178
Thanks idle, thats the sort of guidance I was looking for.  I ultimately needed to get a lot of variables out from the do_work and wasnt sure how to do this without global variables.   Thats also why I was going for the module.   It needs to output to other than the ofrm.   Any ideas how I would handle multiple cariable from the do_work?  Thanks again for the reply
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 38745206
You can pass out ANYTHING in the ReportProgress() method.  For instance, you can make a Class or Structure that holds all the values and pass an instance of that out.  You'd use the overload of ReportProgress() that accepts TWO parameters, and use the second param:
http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx

Then, in the ProgressChanged() event, you cast "e.UserState" back to whatever was passed out in ReportProgress() and you've got all your values.
0

Featured Post

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!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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