本例是利用c#中的性能計數器(performancecounter)監控網絡的狀態。并能夠直觀的展現出來
涉及到的知識點:
performancecounter,表示 windows nt 性能計數器組件。nextvalue() 即獲取計數器樣本并為其返回計算所得值。performancecountercategory 表示性能對象,它定義性能計數器的類別。通過這兩個即可得到計數器的信息。
chart 圖表,vs自帶的chart圖表,大大簡化了對圖表的開發。關于chart,此前已有例子說明。
queue 隊列表示對象的先進先出集合。關于queue此前已有例子說明。
treeview 顯示標記項的分層集合,每個標記項用一個 system.windows.forms.treenode 來表示。即vs自帶的樹狀菜單
timer 實現按用戶定義的時間間隔引發事件的計時器。此計時器最宜用于 windows 窗體應用程序中,并且必須在窗口中使用。定時刷新計數器中的值。
效果圖如下:
關于可用的計數器列表【計數器有很多,一級菜單是計數器的類別,二級菜單是計數器instancename,三級菜單是計數器名稱】,如下圖所示:
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
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 重置性能計數器。如下圖所示:
如果依然不行,嘗試以管理員身份運行【勾上】,如下圖所示:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。