Link to home
Start Free TrialLog in
Avatar of derek7467

asked on and using a background worker

I developed a program that uses WMI to retrieve processes running on a remote workstation.  The problem is it takes about 15 seconds to fully load everything.  My solution is running that method in a background thread so that the user GUI isnt completely frozen making it look like my app is broke.  I am using the below code to run this in the background worker.  ( The example i am using from works fine).  I get an error with the below code (Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on.)

Imports System.ComponentModel
Imports System.Management

Public Class Form1
    Private bw As BackgroundWorker = New BackgroundWorker

    Public Sub New()
        bw.WorkerReportsProgress = True
        bw.WorkerSupportsCancellation = True
        AddHandler bw.DoWork, AddressOf BackgroundWorker1_DoWork
        AddHandler bw.ProgressChanged, AddressOf BackgroundWorker1_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf BackgroundWorker1_RunWorkerCompleted

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        'For i = 1 To 10
        'If bw.CancellationPending = True Then
        '    e.Cancel = True
        '    Exit For
        ' Perform a time consuming operation and report progress.
        'bw.ReportProgress(i * 10)
        Dim RC As String = ""

            Dim theScope As New ManagementScope("\\" & RC & "\root\cimv2")
            Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Process")
            ListView1.Groups.Add(New ListViewGroup("Processes", "Process List"))
            For Each managementObject As ManagementObject In (New ManagementObjectSearcher(theScope, objectQuery)).[Get]()
                Dim item As ListViewItem = ListView1.Items.Add(managementObject("Name").ToString())
                item.Group = ListView1.Groups("Processes")
                'End If

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.tbProgress.Text = e.ProgressPercentage.ToString() & "%"

    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If e.Cancelled = True Then
            Me.tbProgress.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            Me.tbProgress.Text = "Error: " & e.Error.Message
            Me.tbProgress.Text = "Done!"
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not bw.IsBusy = True Then
        End If

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If bw.WorkerSupportsCancellation = True Then
        End If
    End Sub
End Class

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I had the same issue. problem is indeed that the background worker process will not have access to any of the visual controls (by that error message)
the solution I had implemented is that the background worker process fills the data into some variable of the form (for example some collection...), and when done (in the event) requests the form to refresh from that collection.
Avatar of it_saige
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of derek7467


AWESOME!!!!!! Thank you!