我在监控应用程序的内存使用方面遇到了一些问题。我已经编写了一些代码,可以通过名称获取进程。但是可能会有多个具有相同名称的进程。所以它只会监视列表中的第一个进程。因此我尝试通过PID来获取它。但是我没有有效的代码。但是这是我在通过名称获取它时使用的代码:
private void SetMemory()
{
PerformanceCounter performanceCounter = new PerformanceCounter
{
CategoryName = "Process",
CounterName = "Working Set",
InstanceName = MinecraftProcess.Process.ProcessName
};
try
{
string text = ((uint)performanceCounter.NextValue() / 1024 / 1000).ToString("N0") + " MB";
MemoryValue.Text = text;
radProgressBar1.Value1 = ((int)performanceCounter.NextValue() / 1024 / 1000);
}
catch (Exception ex)
{
}
}
编辑:我有进程ID。但是我不知道如何从那里开始监视。