重新啟動。vbnetfor循環(huán)占用cpu問題只要重新啟動就可以了。VBNET是一種簡單,現(xiàn)代,面向?qū)ο蟮挠嬎銠C編程語言,它由微軟開發(fā),將NETFramework和公共語言運行庫的強大功能相結(jié)合。
成都創(chuàng)新互聯(lián)公司10多年企業(yè)網(wǎng)站制作服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站制作及推廣,對成都發(fā)電機回收等多個行業(yè)擁有豐富的網(wǎng)站制作經(jīng)驗的網(wǎng)站建設(shè)公司。
使用wmi
類“Win32_Processor”中LoadPercentage屬性為當(dāng)前的cpu使用率
示例代碼: Private Sub Timer1_Timer()
Dim WMI服務(wù) As Object
Dim 對象 As Object
Dim 子對象 As Object
Dim 電腦名 As String
Dim 刷新 As Long
刷新 = 0
電腦名 = "." '表示本地計算機
Set WMI服務(wù) = GetObject("winmgmts://" 電腦名 "/root/cimv2")
Set 對象 = WMI服務(wù).InstancesOf("Win32_Processor")
Me.CurrentX = 0
Me.CurrentY = 0
For Each 子對象 In 對象
If 刷新 = 0 Then
刷新 = 1
Me.Cls
End If
Me.Print 子對象.Name "[" 子對象.CurrentClockSpeed "Hz] 使用率:" _
子對象.LoadPercentage "%"
Next
End Sub
'
'?。?!重要:首先要添加“引用”一個dll,選擇“System?Management”;
'
Imports?Microsoft.VisualBasic
Imports?System
Imports?System.Collections.Generic
Imports?System.ComponentModel
Imports?System.Data
Imports?System.Drawing
Imports?System.Text
Imports?System.Windows.Forms
Imports?System.Management
Imports?System.IO
Namespace?WindowsApplication1
Partial?Public?Class?Form1
Inherits?Form
Public?Sub?New()
InitializeComponent()
End?Sub
Private?Sub?button1_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取CPU編號
Dim?[MyClass]?As?New?ManagementClass("Win32_Processor")
Dim?MyCollection?As?ManagementObjectCollection?=?[MyClass].GetInstances()
Dim?MyInfo?As?String?=?"當(dāng)前系統(tǒng)CPU編號是:"
Dim?MyCPUID?As?String?=?""
For?Each?MyObject?As?ManagementObject?In?MyCollection
MyCPUID?=?MyObject.Properties("ProcessorId").Value.ToString()
Exit?For
Next?MyObject
MyInfo?=?MyCPUID
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button2_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的當(dāng)前電壓
Dim?MyInfo?As?String?=?"計算機CPU的當(dāng)前電壓是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
Try
MyInfo?=?ControlChars.Lf??String.Format("CurrentVoltage?:?"??MyObject("CurrentVoltage").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Catch
End?Try
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button3_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的外部頻率
Dim?MyInfo?As?String?=?"計算機CPU的外部頻率是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
Try
MyInfo?=?ControlChars.Lf??String.Format("ExtClock?:?"??MyObject("ExtClock").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Catch
End?Try
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button4_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的二級緩存
Dim?MyInfo?As?String?=?"計算機CPU的二級緩存尺寸是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("L2CacheSize:?"??MyObject("L2CacheSize").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button5_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的制造商名稱
Dim?MyInfo?As?String?=?"計算機CPU的制造商名稱是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("Manufacturer?:?"??MyObject("Manufacturer").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button6_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的產(chǎn)品名稱
Dim?MyInfo?As?String?=?"計算機CPU的產(chǎn)品名稱是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("Name?:?"??MyObject("Name").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button7_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的版本信息
Dim?MyInfo?As?String?=?"計算機CPU的版本信息如下:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("Version:?"??MyObject("Version").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button8_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的當(dāng)前使用百分比?注意要把SQLserver或者其他耗CPU的軟件開著否則看不到效果就一直為0
Dim?MyInfo?As?String?=?"計算機CPU的當(dāng)前使用百分比是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("LoadPercentage?:?"??MyObject("LoadPercentage").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button9_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的最大時鐘頻率
Dim?MyInfo?As?String?=?"計算機CPU的最大時鐘頻率是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("MaxClockSpeed?:?"??MyObject("MaxClockSpeed").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button10_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機CPU的當(dāng)前時鐘頻率
Dim?MyInfo?As?String?=?"計算機CPU的當(dāng)前時鐘頻率是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("CurrentClockSpeed?:?"??MyObject("CurrentClockSpeed").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button11_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機的CPU地址寬度
Dim?MyInfo?As?String?=?"當(dāng)前計算機的CPU地址寬度是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("AddressWidth:?"??MyObject("AddressWidth").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
Private?Sub?button14_Click(ByVal?sender?As?Object,?ByVal?e?As?EventArgs)
'獲取計算機的CPU數(shù)據(jù)寬度
Dim?MyInfo?As?String?=?"當(dāng)前計算機的CPU數(shù)據(jù)寬度是:"
Dim?MySearcher?As?New?ManagementObjectSearcher("SELECT?*?FROM?Win32_Processor")
For?Each?MyObject?As?ManagementObject?In?MySearcher.Get()
MyInfo?=?ControlChars.Lf??String.Format("DataWidth?:?"??MyObject("DataWidth").ToString())
MyInfo?=?ControlChars.Lf??"========================================================="
Next?MyObject
MessageBox.Show(MyInfo,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information)
End?Sub
End?Class