代码拉取完成,页面将自动刷新
using System;
using System.Runtime.InteropServices;
namespace LibSystemInfo
{
public static class CPUWinLoadValue
{
private const int SYSTEM_PERFORMANCEINFORMATION = 2;
private const int SYSTEM_TIMEINFORMATION = 3;
private const int NO_ERROR = 0;
public static double CPULOAD = 0f;
private static long oldIdleTime;
private static long oldSystemTime;
private static double processorCount;
static CPUWinLoadValue()
{
processorCount = Environment.ProcessorCount;
byte[] timeInfo = new byte[32];
byte[] perfInfo = new byte[312];
int ret;
ret = NtQuerySystemInformation(SYSTEM_TIMEINFORMATION, timeInfo, timeInfo.Length, IntPtr.Zero);
if (ret != NO_ERROR)
{
throw new NotSupportedException();
}
ret = NtQuerySystemInformation(SYSTEM_PERFORMANCEINFORMATION, perfInfo, perfInfo.Length, IntPtr.Zero);
if (ret != NO_ERROR)
{
throw new NotSupportedException();
}
oldIdleTime = BitConverter.ToInt64(perfInfo, 0);
oldSystemTime = BitConverter.ToInt64(timeInfo, 8);
}
[DllImport("ntdll.dll", EntryPoint = "NtQuerySystemInformation")]
private static extern int NtQuerySystemInformation(int dwInfoType, byte[] lpStructure, int dwSize,
IntPtr returnLength);
public static double Refresh()
{
var f = Query();
if (f < 0)
{
f = 0;
}
if (f > 100)
{
f = 100;
}
CPULOAD = Math.Round(f, 2);
return CPULOAD;
}
public static double Query()
{
byte[] timeInfo = new byte[32];
byte[] perfInfo = new byte[312];
double dbIdleTime, dbSystemTime;
int ret;
ret = NtQuerySystemInformation(SYSTEM_TIMEINFORMATION, timeInfo, timeInfo.Length, IntPtr.Zero);
if (ret != NO_ERROR)
throw new NotSupportedException();
ret = NtQuerySystemInformation(SYSTEM_PERFORMANCEINFORMATION, perfInfo, perfInfo.Length, IntPtr.Zero);
if (ret != NO_ERROR)
throw new NotSupportedException();
dbIdleTime = BitConverter.ToInt64(perfInfo, 0) - oldIdleTime;
dbSystemTime = BitConverter.ToInt64(timeInfo, 8) - oldSystemTime;
if (dbSystemTime != 0)
dbIdleTime = dbIdleTime / dbSystemTime;
dbIdleTime = 100.0 - dbIdleTime * 100.0 / processorCount + 0.5;
oldIdleTime = BitConverter.ToInt64(perfInfo, 0);
oldSystemTime = BitConverter.ToInt64(timeInfo, 8);
return dbIdleTime;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。