using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Counter
{
public partial class Form1 : Form
{
private PerformanceCounter mCounter;//當前計數器
private Queue<double> dataQueue = new Queue<double>(100);//初始化隊列
public Form1()
{
InitializeComponent();
InitCounterCategory();
InitChart();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 初始化計數器信息
/// </summary>
private void InitCounterCategory()
{
//獲取全部的計數器類別
var counterCategories = PerformanceCounterCategory.GetCategories().OrderBy(p => p.CategoryName);
int i = 0;
foreach (var counterCategory in counterCategories)
{
//屬於線程級別的不顯示
if (counterCategory.CategoryName == "Thread")
{
continue;
}
//將信息綁定的TreeView上
this.tvCategory.CheckBoxes = true;
this.tvCategory.Nodes.Add(counterCategory.CategoryName);
string[] instanceNames = counterCategory.GetInstanceNames();
int j = 0;
foreach (var instanceName in instanceNames)
{
this.tvCategory.Nodes[i].Nodes.Add(instanceName);
var counters = counterCategory.GetCounters(instanceName).Select(p => string.Format("{0}", p.CounterName));
int k = 0;
foreach (var counter in counters)
{
this.tvCategory.Nodes[i].Nodes[j].Nodes.Add(counter);
k++;
}
j++;
}
i++;
}
//初始化Counter
PerformanceCounter pc = new PerformanceCounter();
pc.CategoryName = "Processor";
pc.CounterName = "% Processor Time";
pc.InstanceName = "_Total";
pc.MachineName = ".";
pc.ReadOnly = true;
mCounter = pc;
mCounter.NextValue();//初始值
}
//<summary>
//初始化圖表
//</summary>
private void InitChart()
{
//定義圖表區域
this.chart1.ChartAreas.Clear();
ChartArea chartArea1 = new ChartArea("C1");
this.chart1.ChartAreas.Add(chartArea1);
//定義存儲和顯示點的容器
this.chart1.Series.Clear();
Series series1 = new Series("S1");
series1.ChartArea = "C1";
this.chart1.Series.Add(series1);
//設置圖表顯示樣式
this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.SharpTriangle;
this.chart1.ChartAreas[0].AxisY.Title = "Kkbps";//座標軸的標題
this.chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
this.chart1.ChartAreas[0].AxisY.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Maximum = 50;
this.chart1.ChartAreas[0].AxisY.Interval = 5;
this.chart1.ChartAreas[0].AxisX.Interval = 5;
this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.SharpTriangle;
this.chart1.ChartAreas[0].AxisX.Title = "Sec";
this.chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
//設置標題
this.chart1.Titles.Clear();
this.chart1.Titles.Add("S01");
this.chart1.Titles[0].Text = "XXX網絡監控顯示";
this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
//設置圖表顯示樣式
this.chart1.Series[0].Color = Color.LightGreen;
this.chart1.Series[0].ChartType = SeriesChartType.Area;//圖表形狀
this.chart1.Series[0].Points.Clear();
}
/// <summary>
/// 啓動定時器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
/// <summary>
/// 中止定時器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
/// <summary>
/// 定時執行函數
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
UpdateQueueValue();
this.chart1.Series[0].Points.Clear();
if (dataQueue.Max() > this.chart1.ChartAreas[0].AxisY.Maximum)
{
this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling(dataQueue.Max() / 10) * 10;
this.chart1.ChartAreas[0].AxisY.Interval = this.chart1.ChartAreas[0].AxisY.Maximum / 10;
}
for (int i = 0; i < dataQueue.Count; i++)
{
this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
}
}
//更新隊列中的值
private void UpdateQueueValue()
{
if (dataQueue.Count > 100)
{
dataQueue.Dequeue();
}
//獲取的值就Byte/s 因此要除以1024
dataQueue.Enqueue(mCounter.NextValue());
}
/// <summary>
/// 當選中複選框時發生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
{
}
/// <summary>
/// 採用遞歸方法修改節點的選中狀態
/// </summary>
/// <param name="nodes"></param>
/// <param name="flag"></param>
private void CheckedStated(TreeNodeCollection nodes, bool flag)
{
}
}
}