W-ZERO3用 CPU使用率取得クラス

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Uchukamen.WZero3
{
    class CPUStatus
    {
        [DllImport("COREDLL.DLL")]
        private extern static uint GetTickCount();
        [DllImport("COREDLL.DLL")]
        private extern static uint GetIdleTime();
        private static uint dwStartTick = 0;
        private static uint dwIdleSt = 0;
        /// <summary>
        /// CPU使用率
        /// </summary>
        /// <returns>uint 0:最小-100:最大</returns>
        public uint GetCPU()
        {
            uint dwStopTick = GetTickCount();
            uint dwIdleEd = GetIdleTime();
            uint idle = ((100 * (dwIdleEd – dwIdleSt)) / (dwStopTick – dwStartTick));
            dwStartTick = dwStopTick;
            dwIdleSt = dwIdleEd;
            uint cpu = 100 – idle;
            Debug.WriteLine(cpu.ToString());
            return cpu;
        }
    }
}
 
呼び出し
            CPUStatus cpuStat = new CPUStatus();
            uint cpu = cpuStat.GetCPU();

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です