Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
性能计数器类别名称?(C#)_C#_Process_Msdn_Performancecounter - Fatal编程技术网

性能计数器类别名称?(C#)

性能计数器类别名称?(C#),c#,process,msdn,performancecounter,C#,Process,Msdn,Performancecounter,我试图在我的C#应用程序中输入一个性能计数器,该应用程序启动另一个进程并检查该启动进程的处理器使用情况。据我所知,performance counter类要求我分配类别名称、计数器名称和进程名称。我可以很容易地得到进程名,但是在互联网上是否有某种列表,其中包含我可以指定的所有可能的类别和计数器名?我试着在MSDN上搜索类似的东西,但什么也没找到 谢谢你的帮助 您可以根据需要分配给它们。性能监视器将简单地显示您选择的任何类别以及您根据特定需要选择的任何计数器名称 CounterCreationDa

我试图在我的C#应用程序中输入一个性能计数器,该应用程序启动另一个进程并检查该启动进程的处理器使用情况。据我所知,performance counter类要求我分配类别名称、计数器名称和进程名称。我可以很容易地得到进程名,但是在互联网上是否有某种列表,其中包含我可以指定的所有可能的类别和计数器名?我试着在MSDN上搜索类似的东西,但什么也没找到


谢谢你的帮助

您可以根据需要分配给它们。性能监视器将简单地显示您选择的任何类别以及您根据特定需要选择的任何计数器名称

CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
ccdc.Add(new CounterCreationData("Counter Title", "Counter Description", PerformanceCounterType.NumberOfItems32));
PerformanceCounterCategory.Create("My Counter Category", "Category Description", PerformanceCounterCategoryType.Unknown, ccdc);

我认为您想知道您可以监控流程的哪些方面。进程性能计数器的列表可用 尽管如此,您可以使用静态方法列出计算机中的所有类别,也可以更具体地为“流程”类别创建PerformanceCategory,并使用获取所有可用计数器的列表。
希望这有帮助。

我创建了一个方法,显示CriGoT在上面写的内容,一个小的快捷方式

    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();
}

:-)

对于那些想要快速浏览并找到所需计数器的人,这里有一个快速表单,其中显示了三个列表框,其中包含
|类别|实例|计数器|
,以及计时器上更新的计数器值。使用过滤器


这个类使用起来太混乱了!为什么他们不使用枚举而不是由这么多复杂字符组成的字符串呢!我的猜测是,这是基于这样一个事实:每个产品团队(Windows、IIS等)“拥有”计数器名称,因此他们可以在任何时间点添加/删除/更改任何名称。除此之外,我们都可以创建自己的计数器集。我认为创建一个计数器来监视自定义数据没有任何意义,您可以通过编程来实现。类别列表非常庞大。此外,每个类别都有自己的计数器名称列表。对机器的这些细节进行如此强大的抽象是很酷的。我只想感谢这个有用的工具,但我发现了一个问题,它在使用不同于英语的语言的计算机上不能正常工作,特别是在类别上。我试图获取内存的实例和计数器,但因为它在我的语言中被称为其他东西,所以我只得到一个空的列表。你可以编辑代码来解释这个吗?嘿@SimonJensen,谢谢你的提醒。我从系统中得到了一个类别列表,看起来很奇怪,它不会返回列表,在这个示例中我没有任何特定于语言的内容。但我也会双语,所以我可以改变我的系统语言并测试它。有空的时候我会设法找出问题所在。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

namespace CountersListPreview
{
    internal static class CounterPreview
    {
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form f = new CountersListPreviewForm();
            Application.Run(f);
        }
    }

    internal class CountersListPreviewForm : Form
    {
        public CountersListPreviewForm()
        {
            InitializeComponent();
        }

        private PerformanceCounterCategory[] allCats;
        private string[] instances;
        private PerformanceCounter[] counters;
        private PerformanceCounter counter;
        private Timer TitleRefreshTimer;

        private void Form1_Load(object sender, EventArgs e)
        {
            allCats = PerformanceCounterCategory.GetCategories();
            listBox1.DataSource = allCats;
            listBox1.DisplayMember = "CategoryName";

            listBox1.SelectedIndexChanged += On_CatChange;
            listBox2.SelectedIndexChanged += On_InstChange;
            listBox3.SelectedIndexChanged += On_CounterChange;

            textBox2.TextChanged += On_CatFilterChanged;
            textBox3.TextChanged += On_InstFilterChanged;
            textBox4.TextChanged += On_CounterFilterChanged;

            TitleRefreshTimer = new Timer();
            TitleRefreshTimer.Tick += On_Timer;
            TitleRefreshTimer.Interval = 500;
            TitleRefreshTimer.Start();
        }

        private void On_Timer(object sender, EventArgs e)
        {
            textBox1.Text = counter != null ? counter.NextValue().ToString() : "";
        }

        // --------------- SELECTION CHANGE ------------------

        private void On_CatChange(object sender, EventArgs e)
        {
            var cat = listBox1.SelectedItem as PerformanceCounterCategory;
            listBox2.DataSource = instances = cat.GetInstanceNames();
        }

        private void On_InstChange(object sender, EventArgs e)
        {
            var cat = listBox1.SelectedItem as PerformanceCounterCategory;
            var inst = listBox2.SelectedItem as string;
            listBox3.DataSource = counters = cat.GetCounters(inst);
            listBox3.DisplayMember = "CounterName";
        }

        private void On_CounterChange(object sender, EventArgs e)
        {
            counter = listBox3.SelectedItem as PerformanceCounter;
            On_Timer(null, null);
        }

        // --------------- FILTERS ------------------

        private void On_CatFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox2.Text;
            listBox1.DataSource = !string.IsNullOrEmpty(filter) 
                ? allCats.Where(cat => cat.CategoryName.ToLower().Contains(filter.ToLower())).ToArray() 
                : allCats;
        }

        private void On_InstFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox3.Text;
            listBox2.DataSource = !string.IsNullOrEmpty(filter) 
                ? instances.Where(inst => inst.ToLower().Contains(filter.ToLower())).ToArray() 
                : instances;
        }

        private void On_CounterFilterChanged(object sender, EventArgs e)
        {
            var filter = textBox4.Text;
            listBox3.DataSource = !string.IsNullOrEmpty(filter) 
                ? counters.Where(c => c.CounterName.ToLower().Contains(filter.ToLower())).ToArray() 
                : counters;
        }

        // --------------- FORM AND LAYOUT ------------------

        private readonly IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null) components.Dispose();
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.listBox2 = new System.Windows.Forms.ListBox();
            this.listBox3 = new System.Windows.Forms.ListBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(12, 38);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(351, 524);
            this.listBox1.TabIndex = 3;
            // 
            // listBox2
            // 
            this.listBox2.FormattingEnabled = true;
            this.listBox2.Location = new System.Drawing.Point(369, 38);
            this.listBox2.Name = "listBox2";
            this.listBox2.Size = new System.Drawing.Size(351, 524);
            this.listBox2.TabIndex = 3;
            // 
            // listBox3
            // 
            this.listBox3.FormattingEnabled = true;
            this.listBox3.Location = new System.Drawing.Point(726, 38);
            this.listBox3.Name = "listBox3";
            this.listBox3.Size = new System.Drawing.Size(351, 524);
            this.listBox3.TabIndex = 3;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(726, 568);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(351, 20);
            this.textBox1.TabIndex = 4;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(606, 571);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(114, 13);
            this.label1.TabIndex = 5;
            this.label1.Text = "Counter Value (500ms)";
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(12, 12);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(351, 20);
            this.textBox2.TabIndex = 4;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(369, 12);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(351, 20);
            this.textBox3.TabIndex = 4;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(726, 12);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(351, 20);
            this.textBox4.TabIndex = 4;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            //this.BackColor = System.Drawing.SystemColors.;
            this.ClientSize = new System.Drawing.Size(1090, 597);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox4);
            this.Controls.Add(this.textBox3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.listBox3);
            this.Controls.Add(this.listBox2);
            this.Controls.Add(this.listBox1);
            //this.ForeColor = System.Drawing.SystemColors.ControlLightLight;
            this.Name = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        private ListBox listBox1;
        private ListBox listBox2;
        private ListBox listBox3;
        private TextBox textBox1;
        private Label label1;
        private TextBox textBox2;
        private TextBox textBox3;
        private TextBox textBox4;
    }
}