C# 在不使用c格式计时器的情况下,如何使用性能计数器?我收到一个错误,比如找不到类别名称

C# 在不使用c格式计时器的情况下,如何使用性能计数器?我收到一个错误,比如找不到类别名称,c#,progress-bar,performancecounter,C#,Progress Bar,Performancecounter,在编写c格式的视频转换程序时,我使用进度条测量转换速度。我实际上是在使用某些技术进行测量,但我想用实际值给用户他使用了多少CPU,也就是进程的速度 if (comboBox1.Text == "mp3") { var convert = new NReco.VideoConverter.FFMpegConverter(); convert.ConvertMedia(VideoPath, M

在编写c格式的视频转换程序时,我使用进度条测量转换速度。我实际上是在使用某些技术进行测量,但我想用实际值给用户他使用了多少CPU,也就是进程的速度

if (comboBox1.Text == "mp3")
                {
                  var convert = new NReco.VideoConverter.FFMpegConverter();
                    convert.ConvertMedia(VideoPath, MusicPath, "mp3");
                    progressBar1.Value = (int)(performanceCounter1.NextValue());
                    label7.Text = "Processor Time: " + progressBar1.Value.ToString() + "%";
                    /*   progressBar1.Value = 80;
                        label7.Text = "% 80";*/
                    MessageBox.Show("converst is okey");
                    progressBar1.Value = (int)(performanceCounter1.NextValue());
                    label7.Text = "Processor Time: " + progressBar1.Value.ToString() + "%";
我使用从inetnet找到的代码完成了这项工作,但我失败了。 我该怎么修理

在这些图片中


首先,我们初始化要捕获的相关CPU计数器,然后单击按钮开始读取性能计数器并增加进度条。forloop和progressbar增量可能与您的情况无关,但我添加它是为了演示整个场景

根据您在评论部分的说明,这将使用performanceCounters的实时信息更新文本框

public PerformanceCounter privateBytes;
public PerformanceCounter gen2Collections;
public Form1()
{

    InitializeComponent();

    var currentProcess = Process.GetCurrentProcess().ProcessName;
    privateBytes =  new PerformanceCounter(categoryName: "Process", counterName: "Private Bytes", instanceName: currentProcess);
    gen2Collections = new PerformanceCounter(categoryName: ".NET CLR Memory", counterName: "# Gen 2 Collections", instanceName: currentProcess);

}
async Task LongRunningProcess()
{

    await Task.Delay(500);

}
private async void button1_Click(object sender, EventArgs e)
{

    for (int i = 0; i <100; i++)
    {
        progressBar1.Value = i;
        textBox1.Text = "privateBytes:" + privateBytes.NextValue().ToString() + " gen2Collections:" + gen2Collections.NextValue().ToString() ;
        await Task.Run(() => LongRunningProcess());
    }

}

注意:还要检查Hans Passant关于

的回答,您是否也有类似System.ComponentModel.ISupportInitializethis.performanceCounter1.begininit的begininit;没有。Amıadd?看看这个,嘿,Zeynep,链接帮了你吗,如果你需要更多帮助,请告诉我我回头看。以及它的工作原理。但是,它有一个问题。事实上,这并不是一个大问题,但它就像一个bug。我正在更新问题图片。请看这个。