我在使用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占用