Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Morning,

I have a back ground worker that pulls remote wmi information. I need to show progress with aprogress bar and i dont know how. I have read the web and looked through using a timer but i just cant get it. I have pasted my code information for you to view. I just need a baisc this is how you do it and then i can move one with the programming.

I do show progress with the status label and i would like the back ground worker to work with that. Thanks for the help.
VB
 Public Sub MachineBasic_BackgroundWorker_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles MachineBasic_BackgroundWorker.DoWork

Dim MyObjSearcher As System.Management.ManagementObjectSearcher
       Dim MyColl As System.Management.ManagementObjectCollection
       Dim MyObj As System.Management.ManagementObject
       Dim MyConOptions As New System.Management.ConnectionOptions
       With MyConOptions
           .Impersonation = System.Management.ImpersonationLevel.Impersonate
           .Authentication = System.Management.AuthenticationLevel.Packet
       End With
       Dim MyMgtScope As System.Management.ManagementScope
       MyMgtScope = New System.Management.ManagementScope("\\" & strComputer & "\root\cimv2", MyConOptions)
       MyMgtScope.Connect()
       If MyMgtScope.IsConnected = False Then
           Exit Sub
       End If
       '----------------------------------------------------------------------------
       'Basic machine INnformation for the left column
       '----------------------------------------------------------------------------
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_OperatingSystem")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               OSNAME = (MyObj("caption").ToString())
               StatusLabel.Text = "Getting Operating System"
               If OSNAME = "Microsoft Windows 7 Enterprise " Then
                   OSNAME = "Windows 7"
               ElseIf OSNAME = "Microsoft Windows 7 Ultimate " Then
                   OSNAME = "Windows 7 Ultimate"
               ElseIf OSNAME = "Microsoft Windows XP Professional" Then
                   OSNAME = "Windows XP"
               End If
           Next
       Catch ex As Exception
           OSNAME = "UnKnown"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_OperatingSystem")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               SERVICEPACK = (MyObj("servicepackmajorversion").ToString())
               StatusLabel.Text = "Getting Service Pack"
               '---------------------------------------------------------------------
               IMAGEDATE = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("InstallDate")))
               StatusLabel.Text = "Getting Image Date"
               '----------------------------------------------------------------------------
               LASTREBOOT = ManagementDateTimeConverter.ToDateTime(CStr(MyObj("LastBootUpTime")))
               StatusLabel.Text = "Getting Last Reboot"
           Next
       Catch ex As Exception
           SERVICEPACK = "Unavailable"
           IMAGEDATE = "Unavailable"
           LASTREBOOT = "Unavailable"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_ComputerSystem")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               LOGGEDINUSER = (MyObj("Username").ToString())
               StatusLabel.Text = "Getting last logged in User"
           Next
       Catch ex As Exception
           LOGGEDINUSER = "No One Is Logged In"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_PageFileUsage")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               PAGEFILE = (MyObj("AllocatedBaseSize").ToString())
               StatusLabel.Text = "Getting Paging File Size"
           Next
       Catch ex As Exception
           PAGEFILE = "Unavailable"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_ComputerSystem")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               DOMAINRESULT = (MyObj("Domain").ToString())
               StatusLabel.Text = "Getting Domain"
               '-----------------------------------------------------
               RAMTOTAL = Reformat_TB_GB_MB(MyObj("TotalPhysicalMemory").ToString())
               StatusLabel.Text = "Getting Total Memory"
           Next
       Catch ex As Exception
           DOMAINRESULT = "Unavailable"
           RAMTOTAL = "Unavailable"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "SELECT * From Win32_LogicalDisk WHERE caption = 'C:'")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               CFREEDRIVESPACE = "C:\" & Reformat_TB_GB_MB(MyObj("Freespace").ToString())
               StatusLabel.Text = "Getting D:\ Drive Free Space"
           Next
       Catch ex As Exception
           CFREEDRIVESPACE = "No Media Connected"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "SELECT * From Win32_LogicalDisk WHERE caption = 'D:'")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               DFREEDRIVESPACE = "C:\" & Reformat_TB_GB_MB(MyObj("Freespace").ToString())
               StatusLabel.Text = "Getting D:\ Drive Free Space"
           Next
       Catch ex As Exception
           DFREEDRIVESPACE = "No Media Connected"
       End Try
       Try
           ' Get data from Win32_ComputerSystem_WMI
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_VideoController Where DeviceID = 'VideoController1'")
           ' Execute the query
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               Dim VideoResHorizontal = (MyObj("CurrenthorizontalResolution").ToString())
               Dim VideoResVertical = (MyObj("CurrentVerticalResolution").ToString())
               VIDEORES = VideoResHorizontal + " x " & VideoResVertical
               StatusLabel.Text = "Checking Video Resolution"
           Next
       Catch ex As Exception
           VIDEORES = "Unavailable"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               If Not IsDBNull(MyObj.GetPropertyValue("IPAddress")) Then
                   Dim IP As Array = MyObj.GetPropertyValue("IPAddress")
                   For i = 0 To LBound(IP)
                       IPADDRESS = IP(i)
                       StatusLabel.Text = "Getting IP Address"
                       '-----------------------------------------------
                       MACADDRESS = (MyObj("Macaddress").ToString())
                       StatusLabel.Text = "Getting MAC Address"
                       '-----------------------------------------------
                       DNSRESULT = (MyObj("DnsDomain").ToString())
                       StatusLabel.Text = "Getting DNS"
                   Next
               End If
           Next
       Catch ex As Exception
           IPADDRESS = "Unknown"
           MACADDRESS = "Unknown"
           DNSRESULT = "Unknown"
       End Try
       Try
           MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
                   "Select * FROM Win32_TimeZone")
           MyColl = MyObjSearcher.Get
           For Each MyObj In MyColl
               TIMEZONE1 = (MyObj("DayLightName").ToString())
               StatusLabel.Text = "Getting Machine's TimeZone"
           Next
       Catch ex As Exception
           TIMEZONE1 = "Unavailable"
       End Try
       StatusLabel.Text = "Search Complete for" + MachineName_TextBox.Text
       If MachineName_TextBox.Text = "" Then
           StatusLabel.Text = "Search Complete for Local Machine"
       End If
   End Sub
Posted
Updated 9-Jan-12 2:51am
v2

1 solution

Have a look at the Progress Changed event

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx[^]

Within your DoWork method, you can call ReportProgress[^]

This will marshall across threads so you can update your UI progress bar with the new values. So

1) Create an event handler for your background worker ProgressChanged event
2) Within DoWork, periodically call ReportProgress[^], passing the percentage completed
3) In your event handler, update the progress bar with the new percentage

To tell it the progress has changed, add this code to your routine. It's YourBackgroundWorkerName.ReportProgress(value)

VB
Try
    MyObjSearcher = New System.Management.ManagementObjectSearcher(MyMgtScope.Path.ToString, _
            "Select * FROM Win32_OperatingSystem")
    MyColl = MyObjSearcher.Get
    For Each MyObj In MyColl
        OSNAME = (MyObj("caption").ToString())
        StatusLabel.Text = "Getting Operating System"
        If OSNAME = "Microsoft Windows 7 Enterprise " Then
            OSNAME = "Windows 7"
        ElseIf OSNAME = "Microsoft Windows 7 Ultimate " Then
            OSNAME = "Windows 7 Ultimate"
        ElseIf OSNAME = "Microsoft Windows XP Professional" Then
            OSNAME = "Windows XP"
        End If
    Next
    MachineBasic_BackgroundWorker.ReportProgress(10)
Catch ex As Exception
    OSNAME = "UnKnown"
End Try


Just add lines like that throughout your DoWork routine and handle the event. So after that bit completes, in your next Try block add a line MachineBasic_BackgroundWorker.ReportProgress(20) ... etc etc
 
Share this answer
 
v2
Comments
Zachary.shupp 9-Jan-12 10:04am    
I have looked through that article and i dont understand how i tell it to show the progress change. could you give an example?
Dylan Morley 9-Jan-12 10:12am    
See my updated answer, make sense?
Zachary.shupp 9-Jan-12 10:24am    
yes it does. so the 10 will actually show 10 percent on the progress bar. Thank you.
Dylan Morley 9-Jan-12 10:26am    
Exactly, yes. Whatever value you call ReportProgress with will be passed to the ProgressChanged event in the ProgressChangedEventArgs.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx

Then, you can just update your bar with the value..

this.progressBar1.Value = e.ProgressPercentage;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900