用vb檢測cpu使用率,用VB檢測CPU使用率

2021-12-21 22:42:45 字數 4091 閱讀 2362

1樓:

可以從登錄檔裡或得

每一秒獲取一次如果滿足條件則退出

2樓:無顏之月

option explicit

'定義相關的api

private declare function ntquerysysteminformation lib "ntdll" (byval dwinfotype as long, byval lpstructure as long, byval dwsize as long, byval dwreserved as long) as long

private declare sub copymemory lib "kernel32" alias "rtlmovememory" (pdst as any, psrc as any, byval bytelen as long)

'相關的常量

private const system_basicinformation = 0&

private const system_performanceinformation = 2&

private const system_timeinformation = 3&

private const no_error = 0

'相關的資料型別

private type large_integer

dwlow as long

dwhigh as long

end type

private type system_performance_information

liidletime as large_integer

dwspare(0 to 75) as long

end type

private type system_basic_information

dwunknown1 as long

ukemaximumincrement as long

upagesize as long

ummnumberofphysicalpages as long

ummlowestphysicalpage as long

ummhighestphysicalpage as long

uallocationgranularity as long

plowestuseraddress as long

pmmhighestuseraddress as long

ukeactiveprocessors as long

bkenumberprocessors as byte

bunknown2 as byte

wunknown3 as integer

end type

private type system_time_information

likeboottime as large_integer

likesystemtime as large_integer

liexptimezonebias as large_integer

ucurrenttimezoneid as long

dwreserved as long

end type

private lidoldidle as large_integer

private lioldsystem as large_integer

private function getcpuusage() as long '這是介面過程

dim sbsysbasicinfo as system_basic_information

dim spsysperforfinfo as system_performance_information

dim stsystimeinfo as system_time_information

dim curidle as currency

dim cursystem as currency

dim lngresult as long

getcpuusage = -1

lngresult = ntquerysysteminformation(system_basicinformation, varptr(sbsysbasicinfo), lenb(sbsysbasicinfo), 0&)

if lngresult <> no_error then exit function

lngresult = ntquerysysteminformation(system_timeinformation, varptr(stsystimeinfo), lenb(stsystimeinfo), 0&)

if lngresult <> no_error then exit function

lngresult = ntquerysysteminformation(system_performanceinformation, varptr(spsysperforfinfo), lenb(spsysperforfinfo), byval 0&)

if lngresult <> no_error then exit function

'計算cpu佔用率

curidle = convertli(spsysperforfinfo.liidletime) - convertli(lidoldidle)

cursystem = convertli(stsystimeinfo.likesystemtime) - convertli(lioldsystem)

if cursystem <> 0 then curidle = curidle / cursystem

curidle = 100 - curidle * 100 / sbsysbasicinfo.bkenumberprocessors + 0.5

getcpuusage = int(curidle)

lidoldidle = spsysperforfinfo.liidletime

lioldsystem = stsystimeinfo.likesystemtime

end function

private function convertli(litoconvert as large_integer) as currency '把large_integer型別的資料轉換成currency型別

copymemory convertli, litoconvert, lenb(litoconvert)

end function

private sub class_initialize() '類初始化

dim stsystimeinfo as system_time_information

dim spsysperforfinfo as system_performance_information

dim lngresult as long

lngresult = ntquerysysteminformation(system_timeinformation, varptr(stsystimeinfo), lenb(stsystimeinfo), 0&)

if lngresult <> no_error then exit sub

lngresult = ntquerysysteminformation(system_performanceinformation, varptr(spsysperforfinfo), lenb(spsysperforfinfo), byval 0&)

if lngresult <> no_error then exit sub

lidoldidle = spsysperforfinfo.liidletime

lioldsystem = stsystimeinfo.likesystemtime

end sub

private sub form_load()

timer1.interval = 1000

end sub

private sub timer1_timer()

me.caption = "當前cpu佔用率:" & getcpuusage & "%"

end sub

心得交流http://hi.baidu.com/wyzy520

CPU使用率過高電腦效能變低,CPU使用率過高 電腦效能變低

電腦出現系統非常緩慢的情況,檢視工作管理員,cpu佔用達到了50 多,但是程序system idle process顯示閒置cpu資源保持在90 以上,使用process explorer工具檢視資源情況,發現一旦有硬碟讀寫,system idle process程序下interrupts程序的cp...

CPU使用率波動厲害

lz的這個溫度在現在的天氣下還是很正常的,至於cpu使用率大可不用相信,我的很多時候也是波動很的。在說了如果是在維修期間有木馬病毒進電腦了,用360檢測不出來那你自己估計也解決不了的。不過說實話360確實不怎麼樣,建議用瑞星殺次吧,不過瑞星個人感覺很賤,要退出時還要輸驗證碼,而且預設開機啟動,也不好...

CPU使用率百分之百,cpu使用率100怎麼辦?

如果你沒有執行大型程式而出現cpu佔用率100 的情況,基本上是中毒了。cpu溫度過高也會導致系統執行速度下降,然後電腦會自動關機,如果強行連續啟動執行,那會導致cpu燒燬。當然,我還是覺得100 cpu佔用是中毒引起的,如果你能確定你的電腦沒中毒,那就是cpu溫度過高而引起的,你去 個window...