帮助进度栏和背景工作者 [英] Help with Progress bars and Background workers
问题描述
早晨,
我有一个后台工作人员,它可以获取远程WMI信息.我需要显示进度条的进度,我不知道如何.我已经阅读了网络并使用计时器进行了浏览,但我无法理解.我已粘贴我的代码信息供您查看.我只需要一个baisc,这就是您的操作方式,然后我可以进行编程了.
我确实显示了状态标签的进度,并且我希望后台工作人员能够与之一起工作.感谢您的帮助.
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.
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
推荐答案
看看进度已更改"事件
http://msdn.microsoft.com/en-us/library/system. componentmodel.backgroundworker.progresschanged.aspx [ ^ ]
在DoWork方法中,您可以调用 ReportProgress [ ^ ]
这将跨线程处理,因此您可以使用新值更新UI进度栏.所以
1)为您的后台工作者ProgressChanged事件创建一个事件处理程序
2)在DoWork中,定期调用 ReportProgress [
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)
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
只需在整个DoWork例程中添加这样的行并处理事件即可.因此,在完成该操作后,在您的下一个Try块中添加一行MachineBasic_BackgroundWorker.ReportProgress(20)...等等等
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
这篇关于帮助进度栏和背景工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!