最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

涉及到的知識(shí)點(diǎn):

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

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

Queue 隊(duì)列表示對(duì)象的先進(jìn)先出集合。關(guān)于Queue此前已有例子說明。

TreeView 顯示標(biāo)記項(xiàng)的分層集合,每個(gè)標(biāo)記項(xiàng)用一個(gè) System.Windows.Forms.TreeNode 來表示。即VS自帶的樹狀菜單

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

效果圖如下:

關(guān)于可用的計(jì)數(shù)器列表【計(jì)數(shù)器有很多,一級(jí)菜單是計(jì)數(shù)器的類別,二級(jí)菜單是計(jì)數(shù)器InstanceName,三級(jí)菜單是計(jì)數(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;//當(dāng)前計(jì)數(shù)器

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

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

    /// <summary>
    /// 初始化計(jì)數(shù)器信息
    /// </summary>
    private void InitCounterCategory() {
      //獲取所有的計(jì)數(shù)器類別
      var counterCategories = PerformanceCounterCategory.GetCategories().OrderBy(p=>p.CategoryName);
      int i=0;
      foreach (var counterCategory in counterCategories) {
        //屬于線程級(jí)別的不顯示
        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)絡(luò)監(jiān)控計(jì)數(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);
      //定義存儲(chǔ)和顯示點(diǎn)的容器
      this.chart1.Series.Clear();
      Series series1 = new Series("S1");
      series1.ChartArea = "C1";
      this.chart1.Series.Add(series1);
      //設(shè)置圖表顯示樣式
      this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.SharpTriangle;
      this.chart1.ChartAreas[0].AxisY.Title = "Kkbps";//坐標(biāo)軸的標(biāo)題
      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;
      //設(shè)置標(biāo)題
      this.chart1.Titles.Clear();
      this.chart1.Titles.Add("S01");
      this.chart1.Titles[0].Text = "XXX網(wǎng)絡(luò)監(jiān)控顯示";
      this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
      this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
      //設(shè)置圖表顯示樣式
      this.chart1.Series[0].Color = Color.LightGreen;
      this.chart1.Series[0].ChartType = SeriesChartType.Area;//圖表形狀
      this.chart1.Series[0].Points.Clear();
    }

    /// <summary>
    /// 啟動(dòng)定時(shí)器
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnStart_Click(object sender, EventArgs e)
    {
      this.timer1.Start();
      
    }

    /// <summary>
    /// 停止定時(shí)器
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnStop_Click(object sender, EventArgs e)
    {
      this.timer1.Stop();
    }

    /// <summary>
    /// 定時(shí)執(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));
      }
    }

     //更新隊(duì)列中的值
    private void UpdateQueueValue()
    {

      if (dataQueue.Count > 100)
      {
        dataQueue.Dequeue();
      }
      //獲取的值就Byte/s 所以要除以1024
      dataQueue.Enqueue(mCounter.NextValue() / (1024));

    }

    /// <summary>
    /// 當(dāng)選中復(fù)選框時(shí)發(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é)點(diǎn)的狀態(tài)保持一致
      CheckedStated(e.Node.Nodes, flag);
    }

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

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

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)

    C#根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-04-04
  • WPF使用HLSL實(shí)現(xiàn)百葉窗動(dòng)畫效果

    WPF使用HLSL實(shí)現(xiàn)百葉窗動(dòng)畫效果

    百葉窗動(dòng)畫是制作PPT時(shí)常用的動(dòng)畫之一,本文將通過實(shí)現(xiàn)百葉窗動(dòng)畫效果的例子介紹在WPF中如何使用ShaderEffect,感興趣的小伙伴可以了解一下
    2023-09-09
  • C#算法之整數(shù)反轉(zhuǎn)

    C#算法之整數(shù)反轉(zhuǎn)

    這篇文章介紹了C#算法之整數(shù)反轉(zhuǎn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • C#圖片添加水印的實(shí)現(xiàn)代碼

    C#圖片添加水印的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了C#給圖片添加水印的實(shí)現(xiàn)代碼,不僅可以為圖片加文字水印,還可以判斷是否是圖片文件,感興趣的小伙伴們可以參考一下
    2016-02-02
  • C#實(shí)現(xiàn)銷售管理系統(tǒng)

    C#實(shí)現(xiàn)銷售管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)銷售管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C#中struct與class的區(qū)別詳解

    C#中struct與class的區(qū)別詳解

    本文主要介紹了C#中struct與class的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • c#唯一值渲染實(shí)例代碼

    c#唯一值渲染實(shí)例代碼

    這篇文章主要介紹了c#唯一值渲染實(shí)例代碼,有需要的朋友可以參考一下
    2013-12-12
  • C#登錄界面代碼詳細(xì)圖文教程

    C#登錄界面代碼詳細(xì)圖文教程

    我們?cè)谑褂肅#做項(xiàng)目的時(shí)候,基本上都需要制作登錄界面,下面這篇文章主要給大家介紹了關(guān)于C#登錄界面代碼的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • C#并行編程Task類用法介紹

    C#并行編程Task類用法介紹

    這篇文章介紹了C#并行編程Task類的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#中Timer使用及解決重入問題

    C#中Timer使用及解決重入問題

    本文主要介紹了C#中Timer使用及解決重入問題的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02

最新評(píng)論

措勤县| 项城市| 武强县| 镇雄县| 江阴市| 黄梅县| 涞水县| 泽普县| 陆丰市| 英吉沙县| 扎赉特旗| 绵阳市| 贵州省| 句容市| 易门县| 凤城市| 化隆| 海原县| 特克斯县| 安龙县| 梨树县| 冷水江市| 永新县| 连州市| 灵武市| 文化| 邢台县| 芷江| 安多县| 海淀区| 铁力市| 阿巴嘎旗| 墨江| 手机| 延安市| 安陆市| 兰坪| 海口市| 舒城县| 漾濞| 新泰市|