C#利用性能计数器监控网络状态
作者:飞翔的月亮 时间:2022-01-05 00:13:53
本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态。并能够直观的展现出来
涉及到的知识点:
PerformanceCounter,表示 Windows NT 性能计数器组件。NextValue() 即获取计数器样本并为其返回计算所得值。PerformanceCounterCategory 表示性能对象,它定义性能计数器的类别。通过这两个即可得到计数器的信息。
Chart 图表,VS自带的Chart图表,大大简化了对图表的开发。关于Chart,此前已有例子说明。
Queue 队列表示对象的先进先出集合。关于Queue此前已有例子说明。
TreeView 显示标记项的分层集合,每个标记项用一个 System.Windows.Forms.TreeNode 来表示。即VS自带的树状菜单
Timer 实现按用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。定时刷新计数器中的值。
效果图如下:
关于可用的计数器列表【计数器有很多,一级菜单是计数器的类别,二级菜单是计数器InstanceName, * 菜单是计数器名称】,如下图所示:
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Diagnostics;
namespace DemoSharp
{
public partial class NetworkMonitor : Form
{
private PerformanceCounter mCounter;//当前计数器
private Queue<double> dataQueue = new Queue<double>(100);//初始化队列
public NetworkMonitor()
{
InitializeComponent();
InitCounterCategory();
InitChart();
}
/// <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
PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface");
string[] iNames = pcCategory.GetInstanceNames();
PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[0]);
//给网络监控计数器赋值
mCounter = pCounters[0];
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() / (1024));
}
/// <summary>
/// 当选中复选框时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
{
bool flag = e.Node.Checked;//取得选中状态,所有子节点的状态保持一致
CheckedStated(e.Node.Nodes, flag);
}
/// <summary>
/// 采用递归方法修改节点的选中状态
/// </summary>
/// <param name="nodes"></param>
/// <param name="flag"></param>
private void CheckedStated(TreeNodeCollection nodes,bool flag) {
if (nodes != null)
{
foreach (TreeNode node in nodes)
{
node.Checked = flag;
CheckedStated(node.Nodes, flag);
}
}
}
}
}
备注:性能计数器类别获取出现异常的解决方案:
在CMD命令窗口中,执行 LODCTR /R 重置性能计数器。如下图所示:
如果依然不行,尝试以管理员身份运行【勾上】,如下图所示:
标签:C#,计数器,网络状态
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android apk 插件启动内存释放问题
2022-05-16 07:26:39
java中List分页的几种方法介绍
2022-03-01 12:04:28
从零开始学springboot整合feign跨服务调用的方法
2023-05-15 18:30:22
![](https://img.aspxhome.com/file/2023/5/81895_0s.png)
Git和Maven的子模块简单实践
2023-09-22 01:45:55
Android软键盘挡住输入框的终极解决方案
2022-04-30 01:16:47
![](https://img.aspxhome.com/file/2023/4/117164_0s.png)
利用C#操作WMI指南
2022-05-07 18:02:42
Java实现Dijkstra输出最短路径的实例
2023-09-01 17:44:02
Android实现固定屏幕显示的方法
2023-05-23 18:29:07
浅谈Java中复制数组的方式
2022-04-14 23:30:27
Java利用TreeUtils工具类实现列表转树
2021-10-02 03:28:00
使用JAVA实现http通信详解
2023-11-12 12:21:12
![](https://img.aspxhome.com/file/2023/4/61924_0s.png)
Android控件View的文字周围添加图标
2023-02-20 04:04:44
![](https://img.aspxhome.com/file/2023/3/103913_0s.jpg)
Java如何把int类型转换成byte
2023-03-13 11:12:39
![](https://img.aspxhome.com/file/2023/6/79816_0s.png)
快速学习六大排序算法
2023-11-02 22:36:19
![](https://img.aspxhome.com/file/2023/5/105175_0s.gif)
springboot结合websocket聊天室实现私聊+群聊
2022-09-25 03:22:23
![](https://img.aspxhome.com/file/2023/9/67609_0s.jpg)
使用Java visualVM监控远程JVM的流程分析
2022-03-19 16:56:02
![](https://img.aspxhome.com/file/2023/2/73062_0s.png)
Java开发工具IntelliJ IDEA安装图解
2022-06-14 02:30:20
![](https://img.aspxhome.com/file/2023/4/69934_0s.png)
Spring Boot从Controller层进行单元测试的实现
2023-07-21 03:07:10
解决spring cloud服务启动之后回到命令行会自动挂掉问题
2022-09-29 13:16:29
Spring boot2+jpa+thymeleaf实现增删改查
2021-06-02 07:21:49
![](https://img.aspxhome.com/file/2023/2/113212_0s.jpg)