Performance Counter Category Names? (C#)

前端 未结 4 953
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 07:20

I\'m trying to program in a performance counter into my C# application that launches another process and checks the processor usage of that launched process. As I understand it,

4条回答
  •  长情又很酷
    2021-02-05 07:44

    I have created a method that show what CriGoT have writted above, a small shortcut.

        private static void GetAllCounters(string categoryFilter)
        {
            var categories = PerformanceCounterCategory.GetCategories();
            foreach (var cat in categories)
            {
                if (categoryFilter != null && categoryFilter.Length > 0)
                {
                    if (!cat.CategoryName.Contains(categoryFilter)) continue;
                }
                Console.WriteLine("Category {0}", cat.CategoryName);
                try
                {
                    var instances = cat.GetInstanceNames();
                    if (instances != null && instances.Length > 0)
                    {
                        foreach (var instance in instances)
                        {
                            //if (cat.CounterExists(instance))
                            //{
                                foreach (var counter in cat.GetCounters(instance))
                                {
                                    Console.WriteLine("\tCounter Name {0} [{1}]", counter.CounterName, instance);
                                }
                            //}
                        }
                    }
                    else
                    {
                        foreach (var counter in cat.GetCounters())
                        {
                            Console.WriteLine("\tCounter Name {0}", counter.CounterName);
                        }
                    }
                }
                catch (Exception)
                {
                    // NO COUNTERS
                }
            }
            Console.ReadLine();
    }
    

    :-)

提交回复
热议问题