代码拉取完成,页面将自动刷新
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;
}
}
}
}
}
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。