Ai
120 Star 767 Fork 310

chatop2020/AKStream

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CPUWinLoadValue.cs 2.79 KB
一键复制 编辑 原始数据 按行查看 历史
chatop 提交于 2021-05-01 00:34 +08:00 . 重新格式化代码
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;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/chatop2020/AKStream.git
git@gitee.com:chatop2020/AKStream.git
chatop2020
AKStream
AKStream
master

搜索帮助