欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#利用性能計數(shù)器監(jiān)控網(wǎng)絡狀態(tài)

 更新時間:2017年01月07日 10:11:15   作者:飛翔的月亮  
這篇文章主要為大家詳細介紹了C#利用性能計數(shù)器監(jiān)控網(wǎng)絡狀態(tài)的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本例是利用C#中的性能計數(shù)器(PerformanceCounter)監(jiān)控網(wǎng)絡的狀態(tài)。并能夠直觀的展現(xiàn)出來

涉及到的知識點:

PerformanceCounter,表示 Windows NT 性能計數(shù)器組件。NextValue() 即獲取計數(shù)器樣本并為其返回計算所得值。PerformanceCounterCategory 表示性能對象,它定義性能計數(shù)器的類別。通過這兩個即可得到計數(shù)器的信息。

Chart 圖表,VS自帶的Chart圖表,大大簡化了對圖表的開發(fā)。關于Chart,此前已有例子說明。

Queue 隊列表示對象的先進先出集合。關于Queue此前已有例子說明。

TreeView 顯示標記項的分層集合,每個標記項用一個 System.Windows.Forms.TreeNode 來表示。即VS自帶的樹狀菜單

Timer 實現(xiàn)按用戶定義的時間間隔引發(fā)事件的計時器。此計時器最宜用于 Windows 窗體應用程序中,并且必須在窗口中使用。定時刷新計數(shù)器中的值。

效果圖如下:

關于可用的計數(shù)器列表【計數(shù)器有很多,一級菜單是計數(shù)器的類別,二級菜單是計數(shù)器InstanceName,三級菜單是計數(shù)器名稱】,如下圖所示:

代碼如下:

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;//當前計數(shù)器

    private Queue<double> dataQueue = new Queue<double>(100);//初始化隊列

    public NetworkMonitor()
    {
      InitializeComponent();
      InitCounterCategory();
      InitChart();
    }

    /// <summary>
    /// 初始化計數(shù)器信息
    /// </summary>
    private void InitCounterCategory() {
      //獲取所有的計數(shù)器類別
      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]);
      //給網(wǎng)絡監(jiān)控計數(shù)器賦值
      mCounter = pCounters[0];
      mCounter.NextValue();//初始值
    }

     //<summary>
     //初始化圖表
     //</summary>
    private void InitChart()
    {
      //定義圖表區(qū)域
      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網(wǎng)絡監(jiān)控顯示";
      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>
    /// 定時執(zhí)行函數(shù)
    /// </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>
    /// 當選中復選框時發(fā)生
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tvCategory_AfterCheck(object sender, TreeViewEventArgs e)
    {
      bool flag = e.Node.Checked;//取得選中狀態(tài),所有子節(jié)點的狀態(tài)保持一致
      CheckedStated(e.Node.Nodes, flag);
    }

    /// <summary>
    /// 采用遞歸方法修改節(jié)點的選中狀態(tài)
    /// </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);
        }
      }
    }
  }
}

備注:性能計數(shù)器類別獲取出現(xiàn)異常的解決方案:

在CMD命令窗口中,執(zhí)行 LODCTR /R 重置性能計數(shù)器。如下圖所示:

如果依然不行,嘗試以管理員身份運行【勾上】,如下圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#連接Mysql實現(xiàn)增刪改查的操作

    C#連接Mysql實現(xiàn)增刪改查的操作

    在IT行業(yè)中,數(shù)據(jù)庫連接是應用程序開發(fā)中的重要環(huán)節(jié),尤其是在使用C#進行Windows或者Web應用開發(fā)時,經(jīng)常需要與各種數(shù)據(jù)庫進行交互,其中就包括廣泛使用的MySQL,本篇將詳細講解如何使用C#語言來連接MySQL數(shù)據(jù)庫,以實現(xiàn)數(shù)據(jù)的讀取、寫入和其他操作
    2024-09-09
  • C#中的Explicit和Implicit詳情

    C#中的Explicit和Implicit詳情

    Implicit提高了代碼的可讀性,但程序員需要自己保證轉(zhuǎn)換不引發(fā)異常且不丟失信息、Explicit可阻止編譯器靜默調(diào)用可能產(chǎn)生意外后果的轉(zhuǎn)換操作。前者更易于使用,后者能向閱讀代碼的每個人清楚地指示您要轉(zhuǎn)換類型,下面就和小編來一起學習吧
    2021-09-09
  • C# 如何在MVC3中取消備用控制器的選擇

    C# 如何在MVC3中取消備用控制器的選擇

    本篇文章介紹了,如何在MVC3中取消備用控制器的選擇的詳細分析。需要的朋友參考下
    2013-05-05
  • C#函數(shù)式編程中的部分應用詳解

    C#函數(shù)式編程中的部分應用詳解

    這篇文章主要介紹了C#函數(shù)式編程中的部分應用詳解,本文講解了何謂函數(shù)式編程、函數(shù)式編程中的部分應用是什么、部分應用的代碼實例,需要的朋友可以參考下
    2015-01-01
  • C#不可變類型深入解析

    C#不可變類型深入解析

    這篇文章主要介紹了C#不可變類型,對于C#程序員深入理解string數(shù)據(jù)類型有很大的幫助,需要的朋友可以參考下
    2014-08-08
  • C#預處理器指令的用法實例分析

    C#預處理器指令的用法實例分析

    這篇文章主要介紹了C#預處理器指令的用法,以實例形式較為詳細的分析了預處理器指令的原理與相應的用法,有助于深入理解C#程序的運行原理,需要的朋友可以參考下
    2014-11-11
  • C#條件語句、循環(huán)語句(if、while)

    C#條件語句、循環(huán)語句(if、while)

    這篇文章主要介紹了C#條件語句、循環(huán)語句(if、while)的用法,學習c#的朋友可以參考下
    2014-03-03
  • C#關閉指定名字進程的方法

    C#關閉指定名字進程的方法

    這篇文章主要介紹了C#關閉指定名字進程的方法,涉及C#進程操作的相關技巧,需要的朋友可以參考下
    2015-05-05
  • C#用表達式樹構(gòu)建動態(tài)查詢的方法

    C#用表達式樹構(gòu)建動態(tài)查詢的方法

    這篇文章主要介紹了C#用表達式樹構(gòu)建動態(tài)查詢的方法,幫助大家更好的理解和學習c#,感興趣的朋友可以了解下
    2020-12-12
  • 淺析WPF中控件拖拽與拖動的實現(xiàn)

    淺析WPF中控件拖拽與拖動的實現(xiàn)

    這篇文章主要為大家詳細介紹了如何在WPF中實現(xiàn)控件拖拽與拖動的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-12-12

最新評論