How to fetch CPU Usage from DOT Net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vijay Ram

    #1

    How to fetch CPU Usage from DOT Net

    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


  • Fergus Cooney

    #2
    Re: How to fetch CPU Usage from DOT Net

    Hi Vijay,

    You need to use one of the Performance Counters:

    Imports System.Diagnost ics

    Dim oPerf1 As New PerformanceCoun ter

    oPerf1.Category Name = "Processor"
    oPerf1.CounterN ame = "% Processor Time"
    oPerf1.Instance Name = "0"

    Dim I As Integer
    For I = 0 To 100
    SomeListBox.Ite ms.Add (oPerf1.NextVal ue)
    Threading.Threa d.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.??


    Comment

    • Gary

      #3
      Re: How to fetch CPU Usage from DOT Net

      You can use the System.Diagnost ics.Performance Counter class. The
      PerformanceCoun ter 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.Diagnost ics.Performance Counter = New
      System.Diagnost ics.Performance Counter()

      myCounter.Categ oryName = "Processor"
      myCounter.Count erName = "% Processor Time"
      myCounter.Insta nceName = "_Total"
      MessageBox.Show (myCounter.Next Value().ToStrin g()) 'cpu usage

      Cheers,
      - Gary -


      "Vijay Ram" <v_ram@yahoo.co m> wrote in message
      news:e$StKPgdDH A.1728@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi All,
      >
      > Is there any way to get the exact CPU Usage which is shown in Task[/color]
      Manager.[color=blue]
      > Any code snippet or ideas will be appreciated.
      >
      > Thanx in advance,
      >
      > Vijay
      >
      >[/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to fetch CPU Usage from DOT Net

        Hello,

        "Vijay Ram" <v_ram@yahoo.co m> schrieb:[color=blue]
        > Is there any way to get the exact CPU Usage which is shown
        > in Task Manager. Any code snippet or ideas will be appreciated.[/color]



        Please do not include ngs in your x-post which are not related to the
        topic of the question.

        --
        Herfried K. Wagner
        MVP ยท VB Classic, VB .NET



        Comment

        • peruser

          #5
          Re: How to fetch CPU Usage from DOT Net

          microsoft.publi c.dotnet.framew ork.adonet
          microsoft.publi c.dotnet.framew ork.aspnet
          microsoft.publi c.dotnet.framew ork.wmi
          microsoft.publi c.dotnet.genera l
          microsoft.publi c.dotnet.langua ges.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
          CounterMultiBas e
          CounterMultiTim er
          CounterMultiTim er100Ns
          CounterMultiTim er100NsInverse
          CounterMultiTim erInverse
          CounterTimer
          CounterTimerInv erse
          CountPerTimeInt erval32
          CountPerTimeInt erval64
          ElapsedTime
          NumberOfItems32
          NumberOfItems64
          NumberOfItemsHE X32
          NumberOfItemsHE X64
          RateOfCountsPer Second32
          RateOfCountsPer Second64
          RawBase
          RawFraction
          SampleBase
          SampleCounter
          SampleFraction
          Timer100Ns
          Timer100NsInver se

          It's all explained at
          http://msdn.microsoft.com/library/de...ClassTopic.asp

          "Vijay Ram" <v_ram@yahoo.co m> wrote in message
          news:e$StKPgdDH A.1728@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Hi All,
          >
          > Is there any way to get the exact CPU Usage which is shown in Task[/color]
          Manager.[color=blue]
          > Any code snippet or ideas will be appreciated.
          >
          > Thanx in advance,
          >
          > Vijay
          >
          >[/color]


          Comment

          • Fergus Cooney

            #6
            Re: How to fetch CPU Usage from DOT Net

            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
            || Timer100NsInver se

            These are all PerformanceCoun ter 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


            Comment

            Working...