How to fetch CPU Usage from DOT Net

  • Thread starter Thread starter Vijay Ram
  • Start date Start date
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
 
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.??
 
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 -
 
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
 
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
 
This is an Of Thread message about an old question about SQL and webpage
special for Fergus.

Hi Fergus,
Did you remember that problem with images in a SQL database that had to be
shown on a Webform, from which we thought the solution would be
MemoryStream. That does not work because there is no control that can accept
a byte(). (For images only Url's are right).

That example that was retruned and that we saw, was given everywhere in the
asp.net group, but that gives a bad uncontrolled picture on a page.

I did look everywhere but all examples on Internet that I did found where
memorystream and picturebox (window form).

But I found in the in asp.group a hind how to make it with script. But with
script and than you can call a page. I did make it for vb.net. language now
and it works nice. So when there is a next one with that question, wait for
me and sign me in.

Cor
 
Hi Cor,

|| Did you remember that problem with images in a SQL database
|| that had to be shown on a Webform, from which we thought the
|| solution would be MemoryStream.

I hope we didn't think that!! :-) The MemoryStream was to avoid having to
use a file on the server. Getting the data to the web page was the main issue,
and for which we had no solution at the time.

|| That does not work because there is no control that can accept a
byte().
|| For images only Url's are right).

Yes, you have to have a Uri that requests an image and set the response
content type appropriately for the image data.

|| The example that was returned and that we saw, was given
|| everywhere in the asp.net group, but that gives a bad
|| uncontrolled picture on a page.

That's interesting. What sort of bad picture?

|| I did make it for vb.net. language now and it works nice.

That's even more interesting. :-)

Could you send me a copy ? And/or you could post it to the end of the
original thread. (I'd see it there. I keep threads marked for ages - just in
case). And a copy to the asp group if they aren't linked to that thread.

Cheers,
Fergus
 
Back
Top