How to fetch CPU Usage from DOT Net

V

Vijay Ram

Hi All,

Is there any way to get the exact CPU Usage which is shown in Task Manager.
Any code snippet or ideas will be appreciated.

Thanx in advance,

Vijay
 
F

Fergus Cooney

Hi Vijay,

You need to use one of the Performance Counters:

Imports System.Diagnostics

Dim oPerf1 As New PerformanceCounter

oPerf1.CategoryName = "Processor"
oPerf1.CounterName = "% Processor Time"
oPerf1.InstanceName = "0"

Dim I As Integer
For I = 0 To 100
SomeListBox.Items.Add (oPerf1.NextValue)
Threading.Thread.Sleep (20)
Next

Regards,
Fergus

ps. You posted to a lot of groups there. I'm not sure how relevant this
question is to ADO and ASP.??
 
G

Gary

You can use the System.Diagnostics.PerformanceCounter class. The
PerformanceCounter component can be used for both reading existing
predefined or custom counters and writing performance data to custom
counters.
This sould get you started:

Dim myCounter As System.Diagnostics.PerformanceCounter = New
System.Diagnostics.PerformanceCounter()

myCounter.CategoryName = "Processor"
myCounter.CounterName = "% Processor Time"
myCounter.InstanceName = "_Total"
MessageBox.Show(myCounter.NextValue().ToString()) 'cpu usage

Cheers,
- Gary -
 
P

peruser

microsoft.public.dotnet.framework.adonet
microsoft.public.dotnet.framework.aspnet
microsoft.public.dotnet.framework.wmi
microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb

Actually, this is a fairly responsible group of groups for a novice dotnet'r
to query (with the exception of the adonet...). And you got a 'general'
answer. Using the Web Matrix supplied Matrix ClassBrowser (free Web Matrix
download from Microsoft!) it took more time for the ClassBrowser to load
than to find the answer to this question.

However, a "way to get the exact CPU Usage" is excluded by the Heisenberg
uncertainty principle (http://www.aip.org/history/heisenberg/p01.htm)! Do
not despair... what you can get is:

AverageBase
AverageCount64
AverageTimer32
CounterDelta32
CounterDelta64
CounterMultiBase
CounterMultiTimer
CounterMultiTimer100Ns
CounterMultiTimer100NsInverse
CounterMultiTimerInverse
CounterTimer
CounterTimerInverse
CountPerTimeInterval32
CountPerTimeInterval64
ElapsedTime
NumberOfItems32
NumberOfItems64
NumberOfItemsHEX32
NumberOfItemsHEX64
RateOfCountsPerSecond32
RateOfCountsPerSecond64
RawBase
RawFraction
SampleBase
SampleCounter
SampleFraction
Timer100Ns
Timer100NsInverse

It's all explained at
http://msdn.microsoft.com/library/d...emDiagnosticsPerformanceCounterClassTopic.asp
 
F

Fergus Cooney

Hi Per,

|| However, a "way to get the exact CPU Usage" is excluded
|| by the Heisenberg uncertainty principle

Lol.
If ever there's a way round that there'll always be the Windows OS
uncertainty principle.

|| AverageBase
|| AverageCount64
||
|| Timer100Ns
|| Timer100NsInverse

These are all PerformanceCounter data types.

If you have Server Explorer in your VS, you can get a list of the actual
counters by opening this window, and expanding the nodes for Servers and your
machine.

There is also a Management Console application, Perfmon.exe, with which
you can examine the available counters.

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top