-1

現在、毎回、たとえば5秒ごとにPCの全体的な使用率のCPU使用率を取得して、CPU使用率の更新された値を取得したいと思います。

そして、5秒ごとに特定のプロセス名のCPU使用率の値を取得します。たとえば、実行時にCPU使用率を取得するプロセスを選択します。

したがって、Form1でプログラムを実行すると、5秒ごとに更新される2つのラベルが表示されます。

これが私のコードです:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace CpuUsage
{
    public partial class Form1 : Form
    {

        private PerformanceCounter theCPUCounter;
        private PerformanceCounter theMemCounter;
        private PerformanceCounter specProcessCPUCounter;
        private float cpuUsage;
        private float memUsage;
        private string processname;

        public Form1()
        {
            InitializeComponent();


                theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
                specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
                processname = specProcessCPUCounter.CounterName;
                cpuUsage = this.theCPUCounter.NextValue();
                memUsage = theMemCounter.NextValue();
                label1.Text = memUsage.ToString();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

デザイナーにlabel1とlabel2があります。CPUCounterの値は常に0です。そして、specProcessCPUCounterを使用して特定のプロセスcpuUsageを取得する方法がわかりません。そして、5秒ごとに更新されるようにする方法がわかりません。

4

1 に答える 1

0

TickWinFormsタイマーを追加し、そのハンドラーのラベルテキストを更新する必要があります。

于 2012-08-28T16:50:40.110 に答える