熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用C#語言獲取CPU利用率

2022-06-13   來源: .NET編程 

  using System;

  using SystemDiagnostics;

  using SystemThreading;

  public class CpuLoadInfo

  {

  // auxiliary print methods

  private static void Say ( string txt )

  {

  ConsoleWriteLine(txt);

  }

  // auxiliary print methods

  private static void Say()

  {

  Say();

  }

  // The main method Command line arguments are ignored

  [STAThread]

  public static void Main()

  {

  Say($Id: CpuLoadInfocsv // :: rz Exp $);

  Say();

  Say(Attempt to create a PerformanceCounter instance:);

  Say(Category name = + CategoryName);

  Say(Counter name = + CounterName);

  Say(Instance name = + InstanceName);

  PerformanceCounter pc

  = new PerformanceCounter(CategoryNameCounterNameInstanceName);

  Say(Performance counter was created);

  Say(Property CounterType: + pcCounterType);

  Say();

  Say(Property CounterHelp: + pcCounterHelp);

  Say();

  Say(Entering measurement loop);

  while (true)

  {

  ThreadSleep(); // wait for second

  float cpuLoad = pcNextvalue();

  Say(CPU load = + cpuLoad + %);

  }

  }

  // constants used to select the performance counter

  private const string CategoryName = Processor;

  private const string CounterName = % Processor Time;

  private const string InstanceName = _Total;

  }

  這是在我計算機上的計算結果

  Entering measurement loop

  CPU load = %

  CPU load = %

  CPU load = %

  CPU load = %

  CPU load = %

  CPU load = %

  CPU load = %

  CPU load = %


From:http://tw.wingwit.com/Article/program/net/201311/13786.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.