Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CPU Usage

CPU Usage

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manojk_batra
    wrote on last edited by
    #1

    :confused:Dear All, I want to check the CPU Usage in C#. May be through Perfprmance counters?? Can I get sample code for that. With Regards Manoj

    H 1 Reply Last reply
    0
    • M manojk_batra

      :confused:Dear All, I want to check the CPU Usage in C#. May be through Perfprmance counters?? Can I get sample code for that. With Regards Manoj

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      What type of CPU usage you want to check is important. You can browse the various performance counters and read their descriptions using the Server Explorer tab in Visual Studio - right-clicking on the perf counter you want and select Properties - or use perfmon.exe o NT-based Windows OSes. An example of just getting values every .5 seconds follows:

      using System;
      using System.Diagnostics;
      using System.Threading;
       
      class Usage
      {
      static void Main()
      {
      using (PerformanceCounter pc = new PerformanceCounter())
      {
      // Use a specific counter for all CPUs.
      pc.CategoryName = "Processor";
      pc.CounterName = "% User Time";
      pc.InstanceName = "_Total";
       
      Console.Error.WriteLine("Total % User Time:");
      Console.Error.WriteLine("Press any key to exit...");
       
      ThreadPool.QueueUserWorkItem(new WaitCallback(Count), pc);
      Console.Read();
       
      Console.Error.WriteLine("Done");
      }
      }
       
      static void Count(object state)
      {
      PerformanceCounter pc = state as PerformanceCounter;
      while (pc != null)
      {
      Console.WriteLine("{0,3:f2}%", pc.NextValue());
      Thread.Sleep(500);
      }
      }
      }

      This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      M 1 Reply Last reply
      0
      • H Heath Stewart

        What type of CPU usage you want to check is important. You can browse the various performance counters and read their descriptions using the Server Explorer tab in Visual Studio - right-clicking on the perf counter you want and select Properties - or use perfmon.exe o NT-based Windows OSes. An example of just getting values every .5 seconds follows:

        using System;
        using System.Diagnostics;
        using System.Threading;
         
        class Usage
        {
        static void Main()
        {
        using (PerformanceCounter pc = new PerformanceCounter())
        {
        // Use a specific counter for all CPUs.
        pc.CategoryName = "Processor";
        pc.CounterName = "% User Time";
        pc.InstanceName = "_Total";
         
        Console.Error.WriteLine("Total % User Time:");
        Console.Error.WriteLine("Press any key to exit...");
         
        ThreadPool.QueueUserWorkItem(new WaitCallback(Count), pc);
        Console.Read();
         
        Console.Error.WriteLine("Done");
        }
        }
         
        static void Count(object state)
        {
        PerformanceCounter pc = state as PerformanceCounter;
        while (pc != null)
        {
        Console.WriteLine("{0,3:f2}%", pc.NextValue());
        Thread.Sleep(500);
        }
        }
        }

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

        M Offline
        M Offline
        manojk_batra
        wrote on last edited by
        #3

        :confused:Thanks you lot. Actually I want to implement cluster. in which I want to check CPU Usage before assigning it to computation. With Regards Manoj

        H 1 Reply Last reply
        0
        • M manojk_batra

          :confused:Thanks you lot. Actually I want to implement cluster. in which I want to check CPU Usage before assigning it to computation. With Regards Manoj

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You can still use the performance counters and should consider taking a brief running average before giving more work to a CPU. CPU usage goes up and down and performance counters only have so much resolution between ticks (no where near as fast as a CPU tick). There is actually APIS for clustering in Windows available either as automation objects (which you can use with a Runtime-Callable Wrapper (RCW) created by tlbimp.exe) or native functions you can P/Invoke. See http://msdn.microsoft.com/library/en-us/mscs/mscs/windows_clustering.asp[^] for details. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups