/ Forside / Teknologi / Udvikling / VB/Basic / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
VB/Basic
#NavnPoint
berpox 2425
pete 1435
CADmageren 1251
gibson 1230
Phylock 887
gandalf 836
AntonV 790
strarup 750
Benjamin... 700
10  tom.kise 610
cpu usage in %
Fra : Gerald Senarclens de~


Dato : 21-01-02 13:36

Hi. Can anyone tell me how to determine the cpu - usage in percent? Thanks,
Gerald



 
 
Christian R. Larsen (21-01-2002)
Kommentar
Fra : Christian R. Larsen


Dato : 21-01-02 15:42

Gerald Senarclens de Grancy <santa@sbox.tugraz.at> skrev i artiklen
<AWT28.201511$gX1.1555790@news.chello.at>...
> Hi. Can anyone tell me how to determine the cpu - usage in percent?
Thanks,
> Gerald

It can't be done in Visual Basic. It's even impossible to determine the
speed of the CPU unless you are able to get this information from the
registry.


Mikkel Bundgaard (21-01-2002)
Kommentar
Fra : Mikkel Bundgaard


Dato : 21-01-02 16:26

"Gerald Senarclens de Grancy" <santa@sbox.tugraz.at>
wrote in message news:AWT28.201511$gX1.1555790@news.chello.at...
> Hi. Can anyone tell me how to determine the cpu - usage in
> percent? Thanks, Gerald
Hi Gerald

If you are using NT then you can use the Performance Data
Helper library. Look at
http://www.netfokus.dk/vbadmincode/winnt.htm#Performance

Otherwise look at this thead on deja.com
http://groups.google.com/groups?hl=da&th=4a33598d4ed2890d&rnum=3
--
Mikkel Bundgaard
IT University of Copenhagen
http://officehelp.gone.dk
Codito, Ergo Sum



Bjarne Østergård (28-01-2002)
Kommentar
Fra : Bjarne Østergård


Dato : 28-01-02 23:41


"Gerald Senarclens de Grancy" <santa@sbox.tugraz.at> wrote in message
news:AWT28.201511$gX1.1555790@news.chello.at...
> Hi. Can anyone tell me how to determine the cpu - usage in percent?
Thanks,
> Gerald


Put this in a module:
______________Module start_____________________

Enum PERF_DETAIL
PERF_DETAIL_NOVICE = 100
PERF_DETAIL_ADVANCED = 200
PERF_DETAIL_EXPERT = 300
PERF_DETAIL_WIZARD = 400
End Enum

Enum PDH_STATUS
PDH_CSTATUS_VALID_DATA = &H0
PDH_CSTATUS_NEW_DATA = &H1
PDH_CSTATUS_NO_MACHINE = &H800007D0
PDH_CSTATUS_NO_INSTANCE = &H800007D1
PDH_MORE_DATA = &H800007D2
PDH_CSTATUS_ITEM_NOT_VALIDATED = &H800007D3
PDH_RETRY = &H800007D4
PDH_NO_DATA = &H800007D5
PDH_CALC_NEGATIVE_DENOMINATOR = &H800007D6
PDH_CALC_NEGATIVE_TIMEBASE = &H800007D7
PDH_CALC_NEGATIVE_VALUE = &H800007D8
PDH_DIALOG_CANCELLED = &H800007D9
PDH_CSTATUS_NO_OBJECT = &HC0000BB8
PDH_CSTATUS_NO_COUNTER = &HC0000BB9
PDH_CSTATUS_INVALID_DATA = &HC0000BBA
PDH_MEMORY_ALLOCATION_FAILURE = &HC0000BBB
PDH_INVALID_HANDLE = &HC0000BBC
PDH_INVALID_ARGUMENT = &HC0000BBD
PDH_FUNCTION_NOT_FOUND = &HC0000BBE
PDH_CSTATUS_NO_COUNTERNAME = &HC0000BBF
PDH_CSTATUS_BAD_COUNTERNAME = &HC0000BC0
PDH_INVALID_BUFFER = &HC0000BC1
PDH_INSUFFICIENT_BUFFER = &HC0000BC2
PDH_CANNOT_CONNECT_MACHINE = &HC0000BC3
PDH_INVALID_PATH = &HC0000BC4
PDH_INVALID_INSTANCE = &HC0000BC5
PDH_INVALID_DATA = &HC0000BC6
PDH_NO_DIALOG_DATA = &HC0000BC7
PDH_CANNOT_READ_NAME_STRINGS = &HC0000BC8
End Enum

Global Const ERROR_SUCCESS = 0

Declare Function PdhVbGetOneCounterPath _
Lib "PDH.DLL" _
(ByVal PathString As String, _
ByVal PathLength As Long, _
ByVal DetailLevel As Long, _
ByVal CaptionString As String) _
As Long

Declare Function PdhVbCreateCounterPathList _
Lib "PDH.DLL" _
(ByVal PERF_DETAIL As Long, _
ByVal CaptionString As String) _
As Long

Declare Function PdhVbGetCounterPathFromList _
Lib "PDH.DLL" _
(ByVal Index As Long, _
ByVal Buffer As String, _
ByVal BufferLength As Long) _
As Long

Declare Function PdhOpenQuery _
Lib "PDH.DLL" _
(ByVal Reserved As Long, _
ByVal dwUserData As Long, _
ByRef hQuery As Long) _
As PDH_STATUS

Declare Function PdhCloseQuery _
Lib "PDH.DLL" _
(ByVal hQuery As Long) _
As PDH_STATUS

Declare Function PdhVbAddCounter _
Lib "PDH.DLL" _
(ByVal QueryHandle As Long, _
ByVal CounterPath As String, _
ByRef CounterHandle As Long) _
As PDH_STATUS

Declare Function PdhCollectQueryData _
Lib "PDH.DLL" _
(ByVal QueryHandle As Long) _
As PDH_STATUS

Declare Function PdhVbIsGoodStatus _
Lib "PDH.DLL" _
(ByVal StatusValue As Long) _
As Long

Declare Function PdhVbGetDoubleCounterValue _
Lib "PDH.DLL" _
(ByVal CounterHandle As Long, _
ByRef CounterStatus As Long) _
As Double
________________End module____________________


Put this in a form and set the controls
______________Form start_____________

Option Explicit

Private Type CounterInfo
hCounter As Long
strName As String
End Type

Dim hQuery As Long
Dim Counters(0 To 99) As CounterInfo
Dim currentCounterIdx As Long
Dim iPerformanceDetail As PERF_DETAIL

Public Sub AddCounter(strCounterName As String, hQuery As Long)
Dim pdhStatus As PDH_STATUS
Dim hCounter As Long

pdhStatus = PdhVbAddCounter(hQuery, strCounterName, hCounter)
Counters(currentCounterIdx).hCounter = hCounter
Counters(currentCounterIdx).strName = strCounterName
currentCounterIdx = currentCounterIdx + 1
End Sub

Private Sub UpdateValues()
Dim dblCounterValue As Double
Dim pdhStatus As Long
Dim strInfo As String
Dim i As Long

PdhCollectQueryData (hQuery)

i = 0 'Only one counter but you can add more

dblCounterValue = _
PdhVbGetDoubleCounterValue(Counters(i).hCounter, pdhStatus)

'Some error checking, make sure the query went through
If (pdhStatus = PDH_CSTATUS_VALID_DATA) _
Or (pdhStatus = PDH_CSTATUS_NEW_DATA) Then
strInfo = "CPU Usage: " & Format$(dblCounterValue, "0.00")
pb1.Value = dblCounterValue
Me.Caption = Format$(dblCounterValue, "0") & "% - CPU Status"
End If

Label1 = strInfo
End Sub

Private Sub Form_Load()
Dim pdhStatus As PDH_STATUS

pdhStatus = PdhOpenQuery(0, 1, hQuery)
If pdhStatus <> ERROR_SUCCESS Then
MsgBox "OpenQuery failed"
End
End If

' Add the processor time query
AddCounter "\Processor(0)\% Processor Time", hQuery
UpdateValues ' Force an immediate display of the counter values
Timer1.Enabled = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
Timer1.Enabled = False
PdhCloseQuery (hQuery)
End Sub

Private Sub Timer1_Timer()
' fires once per second, can be changed.
UpdateValues
End Sub

______________End form____________________

Thats all

I have testet it on winXP Prof.

Med venlig hilsen
Bjarne Østergård






Søg
Reklame
Statistik
Spørgsmål : 177557
Tips : 31968
Nyheder : 719565
Indlæg : 6408885
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste