Ai
120 Star 767 Fork 310

chatop2020/AKStream

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CPULinuxLoadValue.cs 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using LibCommon;
namespace LibSystemInfo
{
static class CPULinuxLoadValue
{
public static double CPULOAD = 0f;
private static ProcessHelper SystemInfoProcessHelper =
new ProcessHelper(p_StdOutputDataReceived, null!, p_Process_Exited!);
static CPULinuxLoadValue()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
SystemInfoProcessHelper.RunProcess("/usr/bin/top", "-b -p0");
}
}
private static void p_Process_Exited(object sender, EventArgs e)
{
SystemInfoProcessHelper.RunProcess("/usr/bin/top", "-b -p0");
}
private static void p_StdOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
if (e.Data.ToString().ToUpper().Contains("CPU(S)"))
{
string tmpStr = e.Data;
string[] tmpStrArr = tmpStr.Split(',', StringSplitOptions.RemoveEmptyEntries);
if (tmpStrArr.Length > 0)
{
foreach (var str in tmpStrArr)
{
if (str.ToLower().Contains("id"))
{
string s = str.TrimEnd(new[] { 'i', 'd' });
s = s.Trim();
if (double.TryParse(s, out double f))
{
CPULOAD = Math.Round(100f - f, 2);
break;
}
}
}
}
}
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/chatop2020/AKStream.git
git@gitee.com:chatop2020/AKStream.git
chatop2020
AKStream
AKStream
master

搜索帮助