MNIHD 2024-08-12 23:15 采纳率: 33.3%
浏览 6
已结题

C#性能计数器的使用

我在使用C#中的性能计数器组件时遇到了问题,我使用数组命令条形统计图重绘时它没有任何反馈
源码见下,运行后条形统计图无任何变化
使用性能计数器值更新图表内容的代码(此处使用了单线程循环)

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int[] indatas = new int[40];
    PerformanceCounter performanceCounter1 = new PerformanceCounter("Processor Information", "% Processor Utility", "_Total");
    while (true)
    {
        lineChart1.Datas = indatas;
        indatas[39] = (int)performanceCounter1.NextValue();
        for (int i = 38; i >= 0; i--)
        {
            indatas[i] = indatas[i + 1];//此处是数组整体值移动,为了更新图表值
        }
        lineChart1.Invalidate();
        Thread.Sleep(500);
    }

以下为控件源码

//using们
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//正文
//属性
/*
datas:数据集
max/min value:显示上限和下限
numberof...line背景上的线数量
其它大多是颜色
*/
private int[] datas = { 10, 5, 10, 5, 15, 20, 30, 50, 10, 10, 20, 30, 10, 40, 20, 30, 50, 80, 90, 20, 40, 30, 40, 10, 60, 80, 10, 30 };

public int[] Datas
{
    get
    {
        return datas;
    }
    set
    {
        datas = value;
        this.Invalidate();
        panel1.Invalidate();
        points = new Point[datas.Length];
    }
}

private Point[] points = new Point[10];

private int maxValue = 100;

private int minValue = 0;

public int MaxValue
{
    get
    {
        return maxValue;
    }
    set
    {
        maxValue = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

public int MinValue
{
    get
    {
        return minValue;
    }
    set
    {
        minValue = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private int numberOfHorizontalLines = 5;

public int NumberOfHorizontalLines
{
    get
    {
        return numberOfHorizontalLines;
    }
    set
    {
        numberOfHorizontalLines = Math.Abs(value);
        this.Invalidate();
        panel1.Invalidate();
    }
}

private int numberOfVerticalLines = 5;

public int NumberOfVerticalLines
{
    get
    {
        return numberOfVerticalLines;
    }
    set
    {
        numberOfVerticalLines = Math.Abs(value);
        this.Invalidate();
        panel1.Invalidate();
    }
}

private Color backLineColor = Color.DarkGray;

private Color lineColor = Color.DodgerBlue;

public Color BackLineColor
{
    get
    {
        return backLineColor;
    }
    set
    {
        backLineColor = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

public Color LineColor
{
    get
    {
        return lineColor;
    }
    set
    {
        lineColor = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private bool isUsingSizeControl = true;

public bool IsUsingSizeControl
{
    get
    {
        return isUsingSizeControl;
    }
    set
    {
        isUsingSizeControl = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private byte fillColorA = 100;

public byte FillColorA
{
    get
    {
        return fillColorA;
    }
    set
    {
        fillColorA = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private DashStyle lineStyle = DashStyle.Solid;

public DashStyle LineStyle
{
    get
    {
        return lineStyle;
    }
    set
    {
        lineStyle = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

private SmoothingMode smoothingMode = SmoothingMode.HighQuality;

public SmoothingMode SmoothingMode
{
    get
    {
        return smoothingMode;
    }
    set
    {
        smoothingMode = value;
        this.Invalidate();
        panel1.Invalidate();
    }
}

//主绘制代码块,这里可能有问题
private void panel1_Paint(object sender, PaintEventArgs e)
{
    for (int i = 0; i < datas.Length; i++)
    {
        points[i] = new Point(panel1.Width / (datas.Length - 1) * i, panel1.Height - (panel1.Height / (maxValue - minValue - 1) * (datas[i] - minValue)));
    }
    Color realfillcolor = Color.FromArgb(fillColorA, lineColor.R, lineColor.G, lineColor.B);
    Graphics graphics = e.Graphics;
    graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    graphics.SmoothingMode = smoothingMode;
    Pen pen = new Pen(backLineColor, 1);
    Pen linepen = new Pen(lineColor, 2);
    linepen.DashStyle = lineStyle;
    Brush brush = new SolidBrush(realfillcolor);
    graphics.DrawLine(pen, 0, 0, panel1.Width - 1, 0);
    graphics.DrawLine(pen, 0, panel1.Height - 1, panel1.Width - 1, panel1.Height - 1);
    graphics.DrawLine(pen, panel1.Width - 1, panel1.Height - 1, panel1.Width - 1, 0);
    graphics.DrawLine(pen, 0, 0, 0, panel1.Height - 1);
    for (int i = 1; i <= numberOfHorizontalLines; i++)
    {
        graphics.DrawLine(pen, 0, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i, panel1.Width - 1, (panel1.Height - 1) / (numberOfHorizontalLines + 1) * i);
    }
    for (int i = 1; i <= numberOfVerticalLines; i++)
    {
        graphics.DrawLine(pen, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, 0, (panel1.Width - 1) / (numberOfVerticalLines + 1) * i, panel1.Height - 1);
    }
    for (int i = 0; i < datas.Length - 1; i++)
    {
        graphics.DrawLine(linepen, points[i], points[i + 1]);
    }
    Point[] polygonpoints = new Point[points.Length + 2];
    polygonpoints.SetValue(new Point(0, this.Height - 1), 0);
    points.CopyTo(polygonpoints, 1);
    polygonpoints.SetValue(new Point(this.Width - 1, this.Height - 1), polygonpoints.Length - 1);
    graphics.FillPolygon(brush, polygonpoints);
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
}

//同下,大小控制
private void LineChart_Load(object sender, EventArgs e)
{
    points = new Point[datas.Length];
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
}

//大小控制,大概不会出问题
private void LineChart_Resize(object sender, EventArgs e)
{
    if (isUsingSizeControl == true)
    {
        this.Height = (maxValue - minValue - 1) * ((this.Height - 1) / (maxValue - minValue - 1)) + 6;
        this.Width = (datas.Length - 1) * ((this.Width - 1) / (datas.Length - 1)) + 6;
    }
    panel1.Width = this.Width - 6;
    panel1.Height = this.Height - 6;
}

我重写了多次代码,但都没有任何效果,我认为可能是数据方面的问题,但不知道在哪里
我希望每隔一段时间,图表能自动更新并显示当前CPU占用

  • 写回答

2条回答 默认 最新

  • 专家-皆知 2024-08-12 23:15
    关注

    参考免费微信小程序:皆我百晓生 作答,经由专家-皆知整理回复,如问题解决,望采纳;如仍有疑问,请评论回复。

    从你给出的代码来看,你在使用性能计数器来更新CPU占用率,并且尝试在图表中显示这个数据。不过,你的图表没有更新,可能是因为你的图表更新逻辑并没有正确地使用性能计数器的数据。这里有几个可能的问题和建议的解决方案:

    1. 性能计数器数据的获取和处理:你使用PerformanceCounter获取CPU占用率,然后在数组中存储这些数据。然而,你没有正确地处理这些数据来更新图表。你需要确保每次获取新的性能计数器的值时,都正确地更新你的图表数据。
    2. 图表数据的更新:在你的代码中,你有一个循环,不断地获取性能计数器的值并尝试更新图表。但是,你的图表更新逻辑似乎并没有正确地实现。你需要确保每次循环时都更新了图表的数据点。
    3. 线程安全问题:你在后台线程中更新UI元素,这可能会导致线程安全问题。你应该使用Invoke来确保UI的更新在正确的线程中进行。

    下面是一个简化的代码示例,展示如何使用性能计数器数据来更新图表:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        PerformanceCounter performanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        while (true)
        {
            int cpuUsage = (int)performanceCounter.NextValue(); // 获取CPU占用率
            lineChart1.Invoke((MethodInvoker)delegate { lineChart1.UpdateChart(cpuUsage); }); // 使用Invoke在UI线程中更新图表
            Thread.Sleep(500); // 等待一段时间再次获取数据
        }
    }
    

    然后在你的图表类中:

    public void UpdateChart(int cpuUsage)
    {
        // 更新图表数据的逻辑,使用cpuUsage作为新的数据点来更新图表。
    }
    

    请注意,这只是一个简化的示例,你可能需要根据你的具体需求进行调整。此外,请确保处理好资源的释放和异常处理等问题。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月23日
  • 创建了问题 8月12日

悬赏问题

  • ¥50 求恶意流量检测系统搭建教程( C++ python C)
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys