新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
重新启动。vbnetfor循环占用cpu问题只要重新启动就可以了。VBNET是一种简单,现代,面向对象的计算机编程语言,它由微软开发,将NETFramework和公共语言运行库的强大功能相结合。
成都创新互联主营贵定网站建设的网络公司,主营网站建设方案,app开发定制,贵定h5成都小程序开发搭建,贵定网站营销推广欢迎贵定等地区企业咨询
使用wmi
类“Win32_Processor”中LoadPercentage属性为当前的cpu使用率
示例代码: Private Sub Timer1_Timer()
Dim WMI服务 As Object
Dim 对象 As Object
Dim 子对象 As Object
Dim 电脑名 As String
Dim 刷新 As Long
刷新 = 0
电脑名 = "." '表示本地计算机
Set WMI服务 = GetObject("winmgmts://" 电脑名 "/root/cimv2")
Set 对象 = WMI服务.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
使用wmi
类“Win32_Processor”中LoadPercentage属性为当前的cpu使用率
示例代码: Private Sub Timer1_Timer()
Dim WMI服务 As Object
Dim 对象 As Object
Dim 子对象 As Object
Dim 电脑名 As String
Dim 刷新 As Long
刷新 = 0
电脑名 = "." '表示本地计算机
Set WMI服务 = GetObject("winmgmts://" 电脑名 "/root/cimv2")
Set 对象 = WMI服务.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
Imports System
Imports System.Management
Module Module1
Sub Main()
Dim scope As New ManagementScope("\\.\root\cimv2")
scope.Connect()
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_Processor")
Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
Dim cpu As ManagementObject
For Each cpu In searcher.Get()
Console.WriteLine("Processor: " cpu("Name"))
Console.WriteLine("Manufacturer: " cpu("Manufacturer"))
Console.WriteLine("Clock frequency: " cpu("MaxClockSpeed") " MHz")
Console.WriteLine("NumberOfLogicalProcessors: " cpu("NumberOfLogicalProcessors"))
Console.WriteLine("NumberOfCores: " cpu("NumberOfCores"))
Console.WriteLine()
Next cpu
Console.WriteLine()
Console.WriteLine("Press any key to quit")
Console.ReadKey()
End Sub
End Module
VB 我不熟,下面是用汇编写的,我运行过可以的。
//--------------------------------
.model small
.586
.stack
.code
idstring db 49 dup('$')
start:
mov ax,@code
mov ds,ax
lea di,idstring
mov eax,80000002h
cpuid
mov dword ptr [di],eax
mov dword ptr [di+4],ebx
mov dword ptr [di+8],ecx
mov dword ptr [di+12],edx
mov eax,80000003h
cpuid
mov dword ptr [di+16],eax
mov dword ptr [di+20],ebx
mov dword ptr [di+24],ecx
mov dword ptr [di+28],edx
mov eax,80000004h
cpuid
mov dword ptr [di+32],eax
mov dword ptr [di+36],ebx
mov dword ptr [di+40],ecx
mov dword ptr [di+44],edx
mov ah,09h
lea dx,idstring
int 21h
mov ah,4ch
int 21h
end start
//-------------------------------------------------------
程序确实可以执行,输出CPU的ID等信息。
注意:DOS下或MS-DOS下使用。
'
'!!!重要:首先要添加“引用”一个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 = "当前系统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的当前电压
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("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的产品名称
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("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的当前使用百分比 注意要把SQLserver或者其他耗CPU的软件开着否则看不到效果就一直为0
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("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的当前时钟频率
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("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 = "当前计算机的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数据宽度
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("DataWidth : " MyObject("DataWidth").ToString())
MyInfo = ControlChars.Lf "========================================================="
Next MyObject
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class