壹般来说图所示,三级菜单是计数器名称】

本例是使用C#中的性能计数器(PerformanceCounter)监察和控制网络的动静。并能够直观的显现出来

本例是选取C#中的品质计数器(PerformanceCounter)监察和控制互连网的情况。并能够直观的呈现出来

图片 1

关联到的知识点:

关系到的知识点:

依据WPF的开源图表控件有广大,当先伍分之三都以静态图片,若是必要绘制CPU使用率这样的动态数据就呈现心有余而力不足,微软开源的DynamicDataDisplay控件弥补了那个不足,为了做个备忘,小编用它来实时绘制CPU使用率曲线,当然,那几个控件也能够绘制动态线图、气泡图和热力图,具体可参照官网示例代码,使用办法非凡不难,上面就一向贴代码,文本末尾提供VS20一3的以身作则代码下载。

  • PerformanceCounter,表示 Windows NT 质量计数器组件。NextValue()
    即获取计数器样本并为其回到计算机技术商量所得值。
  • PerformanceCounterCategory
    代表质量对象,它定义品质计数器的系列。通过那多个即可得到计数器的消息。
  • Chart
    图表,VS自带的Chart图表,大大简化了对图纸的开发。关于Chart,以前已有例子表明
  • Queue 队列表示对象的先进先出集合。关于Queue在此以前已有例子表明
  • TreeView 展现标记项的分段集合,每种标记项用叁个System.Windows.Forms.TreeNode 来代表。即VS自带的树状菜单
  • Timer 达成按用户定义的时光距离引发事件的计时器。此计时器最宜用于
    Windows 窗体应用程序中,并且必须在窗口中运用。定时刷新计数器中的值。
  • PerformanceCounter,表示 Windows NT 品质计数器组件。NextValue()
    即获取计数器样本并为其回来计算机技术商量所得值。
  • PerformanceCounterCategory
    表示品质对象,它定义质量计数器的花色。通过那七个即可取得计数器的音讯。
  • Chart
    图表,VS自带的Chart图表,大大简化了对图纸的开发。关于Chart,以前已有例子表明
  • Queue 队列表示对象的先进先出集合。关于Queue在此以前已有例子表明
  • TreeView 彰显标记项的分段集合,每种标记项用一个System.Windows.Forms.TreeNode 来代表。即VS自带的树状菜单
  • 提姆er 落成按用户定义的时刻间隔引发轩然大波的计时器。此计时器最宜用于
    Windows 窗体应用程序中,并且必须在窗口中动用。定时刷新计数器中的值。

1、首先需要在档次中援引DynamicDataDisplay.dll,该程序集可经过官网下载大概NuGet形式搜索获去,在包控制台执行以下命令即可自动引用。



PM> Install-Package DynamicDataDisplay

功效图如下:

功能图如下:

2、在XAML文件中引用D三命名空间,同时加上3个名称叫ChartPlotter的图形控件。

图片 2

图片 3

<Window x:Class="WpfCpuUsageSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
        Title="MainWindow" Loaded="Window_Loaded" Height="350" Width="525">
    <Grid>
        <d3:ChartPlotter Grid.Column="0" Grid.Row="0"  Name="plotter"/>
    </Grid>
</Window>

至于可用的计数器列表【计数器有许多,一流菜单是计数器的品种,二级菜单是计数器InstanceName,三级菜单是计数器名称】,如下图所示:

至于可用的计数器列表【计数器有很多,超级菜单是计数器的类型,二级菜单是计数器InstanceName,三级菜单是计数器名称】,如下图所示:

三、在后台窗体类中表明数据源ObservableDataSource用于为图表控件提供数据,PerformanceCounter是性质计数器,可通过它来获取CPU使用率,DispatcherTimer是八个基于WPF的计时器,扶助在子线程异步布告UI线程,在窗口加载事件中初阶化那个类,并未有计时器绑定2个处理程序,用于获取CPU使用率,并将最近的使用率参加到动态曲线中。

图片 4

图片 5

public partial class MainWindow : Window
{
    private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();
    private PerformanceCounter performanceCounter = new PerformanceCounter();
    private DispatcherTimer dispatcherTimer = new DispatcherTimer();
    private int currentSecond = 0;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        plotter.AddLineGraph(dataSource, Colors.Green, 2, "Percentage");
        plotter.LegendVisible = false;
        dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
        dispatcherTimer.Tick += timer_Tick;
        dispatcherTimer.IsEnabled = true;
        plotter.Viewport.FitToView();
    }

    private void timer_Tick(object sender, EventArgs e)
    {
        performanceCounter.CategoryName = "Processor";
        performanceCounter.CounterName = "% Processor Time";
        performanceCounter.InstanceName = "_Total";

        double x = currentSecond;
        double y = performanceCounter.NextValue();

        Point point = new Point(x, y);
        dataSource.AppendAsync(base.Dispatcher, point);

        currentSecond++;
    }
}

代码如下:

代码如下:

图片 6图片 7

图片 8图片 9

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Windows.Forms.DataVisualization.Charting;
 10 using System.Diagnostics;
 11 
 12 namespace DemoSharp
 13 {
 14     public partial class NetworkMonitor : Form
 15     {
 16         private PerformanceCounter mCounter;//当前计数器
 17 
 18         private Queue<double> dataQueue = new Queue<double>(100);//初始化队列
 19 
 20         public NetworkMonitor()
 21         {
 22             InitializeComponent();
 23             InitCounterCategory();
 24             InitChart();
 25         }
 26 
 27         /// <summary>
 28         /// 初始化计数器信息
 29         /// </summary>
 30         private void InitCounterCategory() {
 31             //获取所有的计数器类别
 32             var counterCategories = PerformanceCounterCategory.GetCategories().OrderBy(p=>p.CategoryName);
 33             int i=0;
 34             foreach (var counterCategory in counterCategories) {
 35                 //属于线程级别的不显示
 36                 if (counterCategory.CategoryName == "Thread") {
 37                     continue;
 38                 }
 39                 //将信息绑定的TreeView上
 40                 this.tvCategory.CheckBoxes = true;
 41                 this.tvCategory.Nodes.Add(counterCategory.CategoryName);
 42                 string[] instanceNames = counterCategory.GetInstanceNames();
 43                 int j = 0;
 44                 foreach (var instanceName in instanceNames) {
 45                     this.tvCategory.Nodes[i].Nodes.Add(instanceName);
 46                     var counters = counterCategory.GetCounters(instanceName).Select(p=>string.Format("{0}",p.CounterName));
 47                     int k = 0;
 48                     foreach (var counter in counters) {
 49                         this.tvCategory.Nodes[i].Nodes[j].Nodes.Add(counter);
 50                         k++;
 51                     }
 52                     j++;
 53                 }
 54                 i++;
 55             }
 56             //初始化Counter
 57             PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface");
 58             string[] iNames = pcCategory.GetInstanceNames();
 59             PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[0]);
 60             //给网络监控计数器赋值
 61             mCounter = pCounters[0];
 62             mCounter.NextValue();//初始值
 63         }
 64 
 65          //<summary>
 66          //初始化图表
 67          //</summary>
 68         private void InitChart()
 69         {
 70             //定义图表区域
 71             this.chart1.ChartAreas.Clear();
 72             ChartArea chartArea1 = new ChartArea("C1");
 73             this.chart1.ChartAreas.Add(chartArea1);
 74             //定义存储和显示点的容器
 75             this.chart1.Series.Clear();
 76             Series series1 = new Series("S1");
 77             series1.ChartArea = "C1";
 78             this.chart1.Series.Add(series1);
 79             //设置图表显示样式
 80             this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.SharpTriangle;
 81             this.chart1.ChartAreas[0].AxisY.Title = "Kkbps";//坐标轴的标题
 82             this.chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
 83             this.chart1.ChartAreas[0].AxisY.Minimum = 0;
 84             this.chart1.ChartAreas[0].AxisY.Maximum = 50;
 85             this.chart1.ChartAreas[0].AxisY.Interval = 5;
 86             this.chart1.ChartAreas[0].AxisX.Interval = 5;
 87             this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.SharpTriangle;
 88             this.chart1.ChartAreas[0].AxisX.Title = "Sec";
 89             this.chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
 90             this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
 91             this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
 92             //设置标题
 93             this.chart1.Titles.Clear();
 94             this.chart1.Titles.Add("S01");
 95             this.chart1.Titles[0].Text = "XXX网络监控显示";
 96             this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
 97             this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
 98             //设置图表显示样式
 99             this.chart1.Series[0].Color = Color.LightGreen;
100             this.chart1.Series[0].ChartType = SeriesChartType.Area;//图表形状
101             this.chart1.Series[0].Points.Clear();
102         }
103 
104         /// <summary>
105         /// 启动定时器
106         /// </summary>
107         /// <param name="sender"></param>
108         /// <param name="e"></param>
109         private void btnStart_Click(object sender, EventArgs e)
110         {
111             this.timer1.Start();
112             
113         }
114 
115         /// <summary>
116         /// 停止定时器
117         /// </summary>
118         /// <param name="sender"></param>
119         /// <param name="e"></param>
120         private void btnStop_Click(object sender, EventArgs e)
121         {
122             this.timer1.Stop();
123         }
124 
125         /// <summary>
126         /// 定时执行函数
127         /// </summary>
128         /// <param name="sender"></param>
129         /// <param name="e"></param>
130         private void timer1_Tick(object sender, EventArgs e)
131         {
132             UpdateQueueValue();
133             this.chart1.Series[0].Points.Clear();
134             if (dataQueue.Max() > this.chart1.ChartAreas[0].AxisY.Maximum) {
135                 this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling(dataQueue.Max() / 10) * 10;
136                 this.chart1.ChartAreas[0].AxisY.Interval = this.chart1.ChartAreas[0].AxisY.Maximum / 10;
137             }
138             for (int i = 0; i < dataQueue.Count; i++)
139             {
140                 this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
141             }
142         }
143 
144          //更新队列中的值
145         private void UpdateQueueValue()
146         {
147 
148             if (dataQueue.Count > 100)
149             {
150                 dataQueue.Dequeue();
151             }
152             //获取的值就Byte/s 所以要除以1024
153             dataQueue.Enqueue(mCounter.NextValue() / (1024));
154 
155         }
156 
157         /// <summary>
158         /// 当选中复选框时发生
159         /// </summary>
160         /// <param name="sender"></param>
161         /// <param name="e"></param>
162         private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
163         {
164             bool flag = e.Node.Checked;//取得选中状态,所有子节点的状态保持一致
165             CheckedStated(e.Node.Nodes, flag);
166         }
167 
168         /// <summary>
169         /// 采用递归方法修改节点的选中状态
170         /// </summary>
171         /// <param name="nodes"></param>
172         /// <param name="flag"></param>
173         private void CheckedStated(TreeNodeCollection nodes,bool flag) {
174             
175             if (nodes != null)
176             {
177                 foreach (TreeNode node in nodes)
178                 {
179                     node.Checked = flag;
180                     CheckedStated(node.Nodes, flag);
181                 }
182             }
183         }
184     }
185 }
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Windows.Forms.DataVisualization.Charting;
 10 using System.Diagnostics;
 11 
 12 namespace DemoSharp
 13 {
 14     public partial class NetworkMonitor : Form
 15     {
 16         private PerformanceCounter mCounter;//当前计数器
 17 
 18         private Queue<double> dataQueue = new Queue<double>(100);//初始化队列
 19 
 20         public NetworkMonitor()
 21         {
 22             InitializeComponent();
 23             InitCounterCategory();
 24             InitChart();
 25         }
 26 
 27         /// <summary>
 28         /// 初始化计数器信息
 29         /// </summary>
 30         private void InitCounterCategory() {
 31             //获取所有的计数器类别
 32             var counterCategories = PerformanceCounterCategory.GetCategories().OrderBy(p=>p.CategoryName);
 33             int i=0;
 34             foreach (var counterCategory in counterCategories) {
 35                 //属于线程级别的不显示
 36                 if (counterCategory.CategoryName == "Thread") {
 37                     continue;
 38                 }
 39                 //将信息绑定的TreeView上
 40                 this.tvCategory.CheckBoxes = true;
 41                 this.tvCategory.Nodes.Add(counterCategory.CategoryName);
 42                 string[] instanceNames = counterCategory.GetInstanceNames();
 43                 int j = 0;
 44                 foreach (var instanceName in instanceNames) {
 45                     this.tvCategory.Nodes[i].Nodes.Add(instanceName);
 46                     var counters = counterCategory.GetCounters(instanceName).Select(p=>string.Format("{0}",p.CounterName));
 47                     int k = 0;
 48                     foreach (var counter in counters) {
 49                         this.tvCategory.Nodes[i].Nodes[j].Nodes.Add(counter);
 50                         k++;
 51                     }
 52                     j++;
 53                 }
 54                 i++;
 55             }
 56             //初始化Counter
 57             PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface");
 58             string[] iNames = pcCategory.GetInstanceNames();
 59             PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[0]);
 60             //给网络监控计数器赋值
 61             mCounter = pCounters[0];
 62             mCounter.NextValue();//初始值
 63         }
 64 
 65          //<summary>
 66          //初始化图表
 67          //</summary>
 68         private void InitChart()
 69         {
 70             //定义图表区域
 71             this.chart1.ChartAreas.Clear();
 72             ChartArea chartArea1 = new ChartArea("C1");
 73             this.chart1.ChartAreas.Add(chartArea1);
 74             //定义存储和显示点的容器
 75             this.chart1.Series.Clear();
 76             Series series1 = new Series("S1");
 77             series1.ChartArea = "C1";
 78             this.chart1.Series.Add(series1);
 79             //设置图表显示样式
 80             this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.SharpTriangle;
 81             this.chart1.ChartAreas[0].AxisY.Title = "Kkbps";//坐标轴的标题
 82             this.chart1.ChartAreas[0].AxisY.TextOrientation = TextOrientation.Rotated270;
 83             this.chart1.ChartAreas[0].AxisY.Minimum = 0;
 84             this.chart1.ChartAreas[0].AxisY.Maximum = 50;
 85             this.chart1.ChartAreas[0].AxisY.Interval = 5;
 86             this.chart1.ChartAreas[0].AxisX.Interval = 5;
 87             this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.SharpTriangle;
 88             this.chart1.ChartAreas[0].AxisX.Title = "Sec";
 89             this.chart1.ChartAreas[0].AxisX.TextOrientation = TextOrientation.Horizontal;
 90             this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
 91             this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
 92             //设置标题
 93             this.chart1.Titles.Clear();
 94             this.chart1.Titles.Add("S01");
 95             this.chart1.Titles[0].Text = "XXX网络监控显示";
 96             this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
 97             this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
 98             //设置图表显示样式
 99             this.chart1.Series[0].Color = Color.LightGreen;
100             this.chart1.Series[0].ChartType = SeriesChartType.Area;//图表形状
101             this.chart1.Series[0].Points.Clear();
102         }
103 
104         /// <summary>
105         /// 启动定时器
106         /// </summary>
107         /// <param name="sender"></param>
108         /// <param name="e"></param>
109         private void btnStart_Click(object sender, EventArgs e)
110         {
111             this.timer1.Start();
112             
113         }
114 
115         /// <summary>
116         /// 停止定时器
117         /// </summary>
118         /// <param name="sender"></param>
119         /// <param name="e"></param>
120         private void btnStop_Click(object sender, EventArgs e)
121         {
122             this.timer1.Stop();
123         }
124 
125         /// <summary>
126         /// 定时执行函数
127         /// </summary>
128         /// <param name="sender"></param>
129         /// <param name="e"></param>
130         private void timer1_Tick(object sender, EventArgs e)
131         {
132             UpdateQueueValue();
133             this.chart1.Series[0].Points.Clear();
134             if (dataQueue.Max() > this.chart1.ChartAreas[0].AxisY.Maximum) {
135                 this.chart1.ChartAreas[0].AxisY.Maximum = Math.Ceiling(dataQueue.Max() / 10) * 10;
136                 this.chart1.ChartAreas[0].AxisY.Interval = this.chart1.ChartAreas[0].AxisY.Maximum / 10;
137             }
138             for (int i = 0; i < dataQueue.Count; i++)
139             {
140                 this.chart1.Series[0].Points.AddXY((i + 1), dataQueue.ElementAt(i));
141             }
142         }
143 
144          //更新队列中的值
145         private void UpdateQueueValue()
146         {
147 
148             if (dataQueue.Count > 100)
149             {
150                 dataQueue.Dequeue();
151             }
152             //获取的值就Byte/s 所以要除以1024
153             dataQueue.Enqueue(mCounter.NextValue() / (1024));
154 
155         }
156 
157         /// <summary>
158         /// 当选中复选框时发生
159         /// </summary>
160         /// <param name="sender"></param>
161         /// <param name="e"></param>
162         private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
163         {
164             bool flag = e.Node.Checked;//取得选中状态,所有子节点的状态保持一致
165             CheckedStated(e.Node.Nodes, flag);
166         }
167 
168         /// <summary>
169         /// 采用递归方法修改节点的选中状态
170         /// </summary>
171         /// <param name="nodes"></param>
172         /// <param name="flag"></param>
173         private void CheckedStated(TreeNodeCollection nodes,bool flag) {
174             
175             if (nodes != null)
176             {
177                 foreach (TreeNode node in nodes)
178                 {
179                     node.Checked = flag;
180                     CheckedStated(node.Nodes, flag);
181                 }
182             }
183         }
184     }
185 }

View Code

View Code

备注:质量计数器体系获取出现相当的消除方案:

备考:质量计数器连串获取现身分外的化解方案:

在CMD命令窗口中,执行 LODCT卡宴 /兰德揽胜极光 重置质量计数器。如下图所示:

在CMD命令窗口中,执行 LODCT智跑 /景逸SUV 重置性能计数器。如下图所示:

图片 10

图片 11

若果仍旧丰裕,尝试以管理人身份运转【勾上】,如下图所示:

假如还是非常,尝试以管理人身份运营【勾上】,如下图所示:

图片 12

图片 13

 ——————————————————————————————————————–

 ——————————————————————————————————————–

相关文章