首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内存PerformanceCounter C#

内存PerformanceCounter C#
EN

Stack Overflow用户
提问于 2020-10-26 21:14:56
回答 1查看 706关注 0票数 0

我在C#的PerformanceCounter考试上遇到了麻烦。当我在远程机器上运行它时,它运行得很好,但是当试图在运行它的机器上运行它时,我会得到以下错误:System.InvalidOperationException:‘无法找到具有指定类别名称'Memory’的性能计数器,计数器名称'% Committed在使用‘’

代码语言:javascript
复制
            int i = 1;
            PerformanceCounter ramPerfCounter = new PerformanceCounter();
            ramPerfCounter.CategoryName = "Memory";
            ramPerfCounter.CounterName = "% Committed Bytes In Use";
            ramPerfCounter.MachineName = "server";
            ramPerfCounter.ReadOnly = true;

            PerformanceCounter cpuPerfCounter = new PerformanceCounter();
            cpuPerfCounter.CategoryName = "Processor";
            cpuPerfCounter.CounterName = "% Processor Time";
            cpuPerfCounter.InstanceName = "_Total";
            cpuPerfCounter.MachineName = "server";
            cpuPerfCounter.ReadOnly = true;

            do
            {

                float fMemory = ramPerfCounter.NextValue();
                float fCpu = cpuPerfCounter.NextValue();

                Console.WriteLine("CPU: " + fCpu.ToString());
                Console.WriteLine("RAM:" + fMemory.ToString());
                Thread.Sleep(1000);
            }
            while (i == 1);

CPU性能计数器工作得很好。

EN

回答 1

Stack Overflow用户

发布于 2020-10-26 21:32:01

可能是几个问题

  1. 您不需要指定运行在本地实例上的MachineName,它用于连接到只读远程实例。

检查您的计算机上是否有计数器可用

“监视工具”性能监视器面板上的资源管理器counter

  • Click
  1. Open Performance
  2. 中的“控制面板\所有控制面板项\管理工具”,单击+按钮从列表中添加新的
  3. 选择内存,并查看可用的计数器。

或者,您可以使用此代码获取类别列表。

代码语言:javascript
复制
public static List<string> GetCategoryNames(string machineName)
{
    List<string> catNameList = new List<string>();
    List<PerformanceCounterCategory> categories = new List<PerformanceCounterCategory>();
    try
    {
        if (!string.IsNullOrEmpty(machineName))
        {
            categories = PerformanceCounterCategory.GetCategories(machineName).ToList();
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories().ToList();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    foreach(PerformanceCounterCategory category in categories)
    {
        catNameList.Add(category.CategoryName);
    }
    catNameList.Sort();
    return catNameList;
}

然后添加一个catNameList.Contains()调用,以检查计数器是否可用。

找到所有你可以使用的计数器

代码语言:javascript
复制
public static Dictionary<string, List<string>> GetPerfCounterNames(string machineName)
{
    Dictionary<string, List<string>> perfCounterList = new Dictionary<string, List<string>>();
    List<PerformanceCounterCategory> categories = new List<PerformanceCounterCategory>();
    try
    {
        if (!string.IsNullOrEmpty(machineName))
        {
            categories = PerformanceCounterCategory.GetCategories(machineName).ToList();
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories().ToList();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    foreach (PerformanceCounterCategory category in categories)
    {
        List<PerformanceCounter> pcList = null;
        if (category.GetInstanceNames().Length > 0)
        {
            pcList = category.GetCounters(category.GetInstanceNames()[0]).ToList();
        }
        else
        {
            pcList = category.GetCounters().ToList();
        }
        List<string> pcNameList = new List<string>();
        foreach (PerformanceCounter pc in pcList)
        {
            pcNameList.Add(pc.CounterName);
        }
        pcNameList.Sort();
        perfCounterList.Add(category.CategoryName, pcNameList);
    }
    return perfCounterList;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64545080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档