dcsimg
Results 1 to 5 of 5

Thread: Background Worker - Simple Example

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Background Worker - Simple Example

    Hi im new to vb.net anyone have a simple example of how to use a background worker thanks

  2. #2
    PowerPoster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,176

    Re: Background Worker - Simple Example

    Please remember next time...elections matter!

  3. #3

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Background Worker - Simple Example

    for some reason the the background worker is not working for me i am calling public functions inside the background workers do work event am i doing something wrong?

    Code:
       Private Sub ToolStripButton1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStripButton1.MouseDown
    
            'RUN THE BACKGROUND WORKER
            Me.BackgroundWorker1.RunWorkerAsync()
        End Sub
    
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            If IsScanning Then
                Exit Sub
            End If
    
            Me.ListView1.Items.Clear()
            ItemCount = 0
            RemovedCount = 0
            CancelScan = False
            IsScanning = True
    
            Try
                Me.ToolStripStatusLabel1.Text = UpdateStatusBar("StartStatus")
                Dim WinDir As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows))
    
                If HardDiskLetter = Nothing Then
                    Call ProcessJunkFiles(Me.ListView1, GetHardDrives())
                    Call ProcessWindowsLogs(Me.ListView1, WinDir)
                    Call ProcessSpecialFolders(Me.ListView1, GetHardDrives())
                Else
                    Call ProcessJunkFiles(Me.ListView1, HardDiskLetter)
                    Call ProcessWindowsLogs(Me.ListView1, WinDir)
                    Call ProcessSpecialFolders(Me.ListView1, HardDiskLetter)
                End If
            Catch ex As Exception
                'err
            End Try
        End Sub
    Last edited by sinner0636; Dec 27th, 2017 at 03:02 PM.

  4. #4
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    888

    Re: Background Worker - Simple Example

    You can't do anything in the DoWork event that touches or modifies anything in the UI, if you need to update the UI then you should be calling the ReportProgress method (https://msdn.microsoft.com/en-us/lib...=vs.110).aspx) and handling the event in the form.

    http://www.vbforums.com/showthread.p...rker-Component is probably worth a read.

    Edit, just realised that link had already been suggested.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: Background Worker - Simple Example

    You should ALWAYS read the relevant documentation first, which means before posting on forums. The Help menu is there for a reason. The documentation for the DoWork event has this to say:
    You must be careful not to manipulate any user-interface objects in your DoWork event handler.
    That thread of mine that you have been directed to provides several examples of updating the UI. Post #4 even says this:
    Here are two examples that contrast using a BackgroundWorker to update the UI as the work is done and using it to update the UI only once the work is complete:
    If you want to make changes to the UI before doing any work then do that before calling RunWorkerAsync.

    In your case, what's the point in calling RunWorkerAsync and then testing IsScanning inside the event handler? Why not test first and not call RunWorkerAsync is a scan is in progress? In fact, why have that IsScanning field at all when you already have the IsBusy property of the BackgroundWorker?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width