PerformanceCounterを使ってCPU使用率を取得する例です。
FormにLabelとTimerコントロールを配置し
100ミリ秒毎に表示しています。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pc.CategoryName = "Processor"; pc.CounterName = "% Processor Time"; pc.InstanceName = "_Total"; // 100ミリ秒間隔でタイマー設定 timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 100; timer1.Enabled = true; } void timer1_Tick(object sender, EventArgs e) { label1.Text = string.Format("{0:P2}", pc.NextValue() / 100F); } } }
コメントを残す