Gra*_*ant 20 c# process cpu-usage
我试图弄清楚如何获得特定进程的CPU使用率,但只能找到与整体 CPU使用率相关的信息.
有谁知道如何以百分比的形式提取特定应用程序的当前CPU使用率?
Eri*_*bal 35
性能计数器 - 进程 - 处理器时间百分比.
很少的示例代码为您提供了这个想法:
using System;
using System.Diagnostics;
using System.Threading;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
PerformanceCounter myAppCpu =
new PerformanceCounter(
"Process", "% Processor Time", "OUTLOOK", true);
Console.WriteLine("Press the any key to stop...\n");
while (!Console.KeyAvailable)
{
double pct = myAppCpu.NextValue();
Console.WriteLine("OUTLOOK'S CPU % = " + pct);
Thread.Sleep(250);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据进程ID查找实例的注意事项:
我不知道有什么更好的方法,希望有人这样做.如果没有,这里有一种方法可以为给定进程ID和进程名称的进程找到正确的实例名称.
"ID Process"在"Process"家庭下面还有另一个性能计数器(PC).它返回实例的PID.因此,如果您已经知道名称(即"chrome"或"myapp"),则可以测试每个实例,直到找到PID的匹配项.
每个实例的命名都很简单:"myapp""myapp#1""myapp#2"......等等.
... new PerformanceCounter("Process", "ID Process", appName, true);
Run Code Online (Sandbox Code Playgroud)
一旦PC的值等于PID,您就找到了正确的值appName.然后,您可以将其appName用于其他计数器.
shy*_*kov 10
我从几个答案中汇编了信息(最明显的是这个),并提出了以下代码,该代码能够根据 Windows 提供的性能计数器信息获取有关当前进程的 CPU 和 RAM 使用情况的信息:
public object GetUsage()
{
// Getting information about current process
var process = Process.GetCurrentProcess();
// Preparing variable for application instance name
var name = string.Empty;
foreach (var instance in new PerformanceCounterCategory("Process").GetInstanceNames())
{
if (instance.StartsWith(process.ProcessName))
{
using (var processId = new PerformanceCounter("Process", "ID Process", instance, true))
{
if (process.Id == (int)processId.RawValue)
{
name = instance;
break;
}
}
}
}
var cpu = new PerformanceCounter("Process", "% Processor Time", name, true);
var ram = new PerformanceCounter("Process", "Private Bytes", name, true);
// Getting first initial values
cpu.NextValue();
ram.NextValue();
// Creating delay to get correct values of CPU usage during next query
Thread.Sleep(500);
dynamic result = new ExpandoObject();
// If system has multiple cores, that should be taken into account
result.CPU = Math.Round(cpu.NextValue() / Environment.ProcessorCount, 2);
// Returns number of MB consumed by application
result.RAM = Math.Round(ram.NextValue() / 1024 / 1024, 2);
return result;
}
Run Code Online (Sandbox Code Playgroud)
在没有任何黑客或猜测的情况下确定实例名称,我也在关注多核。
检索到的信息与我在 VS 的进程资源管理器和性能窗口中看到的信息一致。
一种不使用PerformanceCounter来计算单个进程的处理器使用率的方法。
using System;
using System.Diagnostics;
namespace cpuusage
{
class Program
{
private static DateTime lastTime;
private static TimeSpan lastTotalProcessorTime;
private static DateTime curTime;
private static TimeSpan curTotalProcessorTime;
static void Main(string[] args)
{
string processName = "OUTLOOK";
Console.WriteLine("Press the any key to stop...\n");
while (!Console.KeyAvailable)
{
Process[] pp = Process.GetProcessesByName(processName);
if (pp.Length == 0)
{
Console.WriteLine(processName + " does not exist");
}
else
{
Process p = pp[0];
if (lastTime == null || lastTime == new DateTime())
{
lastTime = DateTime.Now;
lastTotalProcessorTime = p.TotalProcessorTime;
}
else
{
curTime = DateTime.Now;
curTotalProcessorTime = p.TotalProcessorTime;
double CPUUsage = (curTotalProcessorTime.TotalMilliseconds - lastTotalProcessorTime.TotalMilliseconds) / curTime.Subtract(lastTime).TotalMilliseconds / Convert.ToDouble(Environment.ProcessorCount);
Console.WriteLine("{0} CPU: {1:0.0}%",processName,CPUUsage * 100);
lastTime = curTime;
lastTotalProcessorTime = curTotalProcessorTime;
}
}
Thread.Sleep(250);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以遍历过程以选择哪个过程,或者如果您已经知道ID,则只需使用此命令而不是GetProcessesByName()
Process p = Process.GetProcessById(123);
Run Code Online (Sandbox Code Playgroud)