rainit2006 / My_Windows

0 stars 0 forks source link

[MyProject]监视电脑状态 #21

Open rainit2006 opened 6 years ago

rainit2006 commented 6 years ago

image

rainit2006 commented 6 years ago
using System;
using System.Diagnostics;

namespace CpuUsage
{
  class Program
  {
    static void Main(string[] args)
    {
      PerformanceCounter cpuCounter = new PerformanceCounter();
      cpuCounter.CategoryName = "Processor";
      cpuCounter.CounterName = "% Processor Time";
      cpuCounter.InstanceName = "_Total";
      PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");

      var unused = cpuCounter.NextValue(); // first call will always return 0
      System.Threading.Thread.Sleep(1000); // wait a second, then try again
      Console.WriteLine("Cpu usage: " + cpuCounter.NextValue() + "%");
      Console.WriteLine("Free ram : " + ramCounter.NextValue() + "MB");

      Console.ReadKey();
    }
  }
}

you might need to call cpuCounter.NextValue() twice to get propper readings.

rainit2006 commented 6 years ago

Add The Percent Into A Progress Bar http://www.dreamincode.net/forums/topic/62979-add-the-percent-into-a-progress-bar/