自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

快速掌握VB.NET獲取CPU使用率技巧

開發(fā) 后端
大家都知道,VB.NET在對(duì)移動(dòng)設(shè)備的處理方面展現(xiàn)了非常大的優(yōu)勢(shì),可以輕松的幫助開發(fā)人員獲取硬盤信息,改變顯示器分辨率等等。那么今天我們就會(huì)對(duì)VB.NET獲取CPU使用率做一個(gè)詳細(xì)的介紹。

大家通過對(duì)VB.NET語言的學(xué)習(xí),會(huì)慢慢的被這款語言強(qiáng)大的功能所吸引住。作為一款完全面向?qū)ο蟮恼Z言,為開發(fā)人員帶來了很多的好處。比如在處理移動(dòng)設(shè)備方面,就極大的體現(xiàn)了它的作用。在這里我們就先來了解一下VB.NET獲取CPU使用率的相關(guān)應(yīng)用技巧。

API聲明

  1. Public Declare Function PdhVbGetOneCounterPath 
    Lib "PDH.DLL" (ByVal PathString As String,
     ByVal PathLength As Integer, ByVal DetailLevel 
    As Integer, ByVal CaptionString As String)As Integer  
  2. Public Declare Function PdhVbCreateCounterPathList 
    Lib "PDH.DLL" (ByVal PERF_DETAIL As Integer, 
    ByVal CaptionString As String) As Integer  
  3. Public Declare Function PdhVbGetCounterPathFromList 
    Lib "PDH.DLL" (ByVal Index As Integer, ByVal Buffer 
    As String, ByVal BufferLength As Integer) As Integer  
  4. Public Declare Function PdhOpenQuery Lib "PDH.DLL" 
    (ByVal Reserved As Integer, ByVal dwUserData As 
    Integer, ByRef hQuery As Integer) As PDH_STATUS  
  5. Public Declare Function PdhCloseQuery Lib "PDH.DLL" 
    (ByVal hQuery As Integer) As PDH_STATUS  
  6. Public Declare Function PdhVbAddCounter Lib "PDH.DLL" 
    (ByVal QueryHandle As Integer, ByVal CounterPath As 
    String, ByRef CounterHandle As Integer) As PDH_STATUS  
  7. Public Declare Function PdhCollectQueryData Lib 
    "PDH.DLL" (ByVal QueryHandle As Integer) As PDH_STATUS  
  8. Public Declare Function PdhVbIsGoodStatus Lib "PDH.DLL" 
    (ByVal StatusValue As Integer) As Integer  
  9. Public Declare Function PdhVbGetDoubleCounterValue Lib 
    "PDH.DLL" (ByVal CounterHandle As Integer, ByRef 
    CounterStatus As Integer) As Double 

枚舉常數(shù)

  1. Enum PERF_DETAIL  
  2. PERF_DETAIL_NOVICE = 100 
  3. PERF_DETAIL_ADVANCED = 200 
  4. PERF_DETAIL_EXPERT = 300 
  5. PERF_DETAIL_WIZARD = 400 
  6. End Enum  
  7. Enum PDH_STATUS  
  8. PDH_CSTATUS_VALID_DATA = &H0S  
  9. PDH_CSTATUS_NEW_DATA = &H1S  
  10. PDH_CSTATUS_NO_MACHINE = &H800007D0  
  11. PDH_CSTATUS_NO_INSTANCE = &H800007D1  
  12. PDH_MORE_DATA = &H800007D2  
  13. PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3  
  14. PDH_RETRY = &H800007D4  
  15. PDH_NO_DATA = &H800007D5  
  16. PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6  
  17. PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7  
  18. PDH_CALC_NEGATIVE_VALUE = &H800007D8  
  19. PDH_DIALOG_CANCELLED = &H800007D9  
  20. PDH_CSTATUS_NO_OBJECT = &HC0000BB8  
  21. PDH_CSTATUS_NO_COUNTER = &HC0000BB9  
  22. PDH_CSTATUS_INVALID_DATA = &HC0000BBA  
  23. PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB  
  24. PDH_INVALID_HANDLE = &HC0000BBC  
  25. PDH_INVALID_ARGUMENT = &HC0000BBD  
  26. PDH_FUNCTION_NOT_FOUND = &HC0000BBE  
  27. PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF  
  28. PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0  
  29. PDH_INVALID_BUFFER = &HC0000BC1  
  30. PDH_INSUFFICIENT_BUFFER = &HC0000BC2  
  31. PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3  
  32. PDH_INVALID_PATH = &HC0000BC4  
  33. PDH_INVALID_INSTANCE = &HC0000BC5  
  34. PDH_INVALID_DATA = &HC0000BC6  
  35. PDH_NO_DIALOG_DATA = &HC0000BC7  
  36. PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8  
  37. End Enum  
  38. Public Const ERROR_SUCCESS As Short = 0 
  39. Public Structure CounterInfo  
  40. Dim hCounter As Integer  
  41. Dim strName As String  
  42. End Structure 

在窗體的load事件里面添加一下語句:

  1. Dim pdhStatus As PDH_STATUS  
  2. pdhStatus = PdhOpenQuery
    (0, 1, hQuery)  
  3. If pdhStatus <> 
    ERROR_SUCCESS Then  
  4. MsgBox("OpenQuery failed")  
  5. End  
  6. End If 

在窗體的closed事件里面添加以下代碼

  1. PdhCloseQuery(hQuery) 

VB.NET獲取CPU使用率之顯示利用率信息

  1. Private Sub UpdateValues()  
  2. Dim dblCounterValue As Double  
  3. Dim pdhStatus As Integer  
  4. Dim strInfo As String  
  5. Dim i As Integer  
  6. PdhCollectQueryData(hQuery)  
  7. i = 0 
  8. dblCounterValue = 
    PdhVbGetDoubleCounterValue
    (Counters(i).hCounter, pdhStatus) 

將VB.NET獲取CPU使用率顯示在標(biāo)簽中

  1. If (pdhStatus = PDH_STATUS.
    PDH_CSTATUS_VALID_DATA) or 
    (
    pdhStatus = PDH_STATUS.PDH_
    CSTATUS_NEW_DATA) Then  
  2. strInfo = "CPU 使用率: " & 
    VB6.Format(dblCounterValue, "0.00")  
  3. pb1.Value = dblCounterValue 
  4. Me.Text = VB6.Format(dblCounterValue,
     "0") & "% - CPU 當(dāng)前狀態(tài)"  
  5. End If  
  6. Label1.Text = strInfo 
  7. End Sub 

【編輯推薦】

  1. VB.NET刪除空白行具體實(shí)現(xiàn)方法詳解
  2. VB.NET連接遠(yuǎn)程數(shù)據(jù)庫技巧分享
  3. VB.NET播放WAV實(shí)現(xiàn)方法介紹
  4. VB.NET嵌入文件操作技巧分享
  5. 深入分析VB.NET FieldOffset特性
責(zé)任編輯:曹凱 來源: CSDN
相關(guān)推薦

2010-01-11 13:33:07

VB.NET使用數(shù)組

2010-01-08 18:16:52

VB.NET變量

2010-01-14 13:59:01

2010-01-11 16:04:10

VB.NET使用wit

2021-11-11 16:46:02

CPU使用率 .NET

2010-01-18 19:36:52

VB.NET調(diào)整控件

2009-11-10 13:43:28

VB.NET Comm

2009-10-27 14:50:25

VB.NET控件數(shù)組

2010-01-18 18:20:49

VB.NET使用API

2009-11-02 13:54:27

VB.NET shel

2009-10-28 16:47:26

VB.NET訪問數(shù)據(jù)庫

2010-01-11 10:08:47

VB.NET事件通道

2009-11-02 15:49:23

VB.NET顯示系統(tǒng)信

2009-10-21 09:40:23

VB.NET搜索

2009-11-02 17:54:44

VB.NET數(shù)組

2009-10-29 14:16:32

VB.NET讀寫文本文

2009-10-09 16:11:33

VB.NET語法

2009-10-29 09:06:26

VB.NET Web

2009-11-10 11:04:09

VB.NET數(shù)據(jù)類型

2010-01-12 10:19:02

VB.NET操作GDI
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)