代码之家  ›  专栏  ›  技术社区  ›  陳柏睿

为什么性能计数器根本不工作

  •  0
  • 陳柏睿  · 技术社区  · 6 年前

    由于某种原因,我的性能计数器不工作。我看到这个性能计数器在其他人的笔记本电脑上工作;桌面,但不是我的。 它没有让我选择的列表,其他人根本没有这个问题。即使我从服务器管理器中捕捉到它,并将其放到表单中,它仍然显示“System.Diagnostic.PerformanceCounter”,这非常恼人。

    using System;
    using System.Diagnostics;
    using Microsoft.WindowsAzure.Diagnostics;
    
    namespace MonitorC
    {
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        //PerformanceCounter perfCPUCounter = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
        //PerformanceCounter perfRAMCounter = new PerformanceCounter("Memory", "Available MBytes");
        //PerformanceCounter perfSysCounter = new PerformanceCounter("System", "System Up Time");
        public Form1()
        {
            InitializeComponent();
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            CPUusage.Text = performanceCounter1.ToString();
            RAMusage.Text = performanceCounter2.ToString();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
    
        }
    }
    }
    

    第一张图片:

    enter image description here

    第二张图片:

    enter image description here

    第三张图片:

    enter image description here

    我拥有的代码:

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   danish    6 年前

    您只需将呼叫添加到 NextSample 获取原始值或 NextValue 获取计算值。这样地:

    performaceCounter1.NextValue().ToString();