CPU效能顯示

      在〈CPU效能顯示〉中尚無留言

CPU 效能顯示

using System;
using System.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Input;
using System.Windows.Threading;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            //dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 100);
            dispatcherTimer.Interval = new TimeSpan(10000000);
            dispatcherTimer.Start();

            //設定DataTable的欄位
            SetDataTable();
            //設定Chart Control
            SetChart();

            this.mainChart.DataSource = dt;
            this.mainChart.DataBind();//這時候先DataBind()是為了顯示空白的圖表
        }

        private void SetDataTable()
        {
            dt.Columns.Add("Processor");
            dt.Columns.Add("Memory");

            //這個動作只是為了能夠在一開始顯示圖表,比例就是30筆
            for (int i = 0; i < 30; i++) { 
                DataRow dr = dt.NewRow(); 
                dr["Processor"] = 0; 
                dt.Rows.Add(dr); 
            } 
        } 
        private void SetChart() 
        { 
            ChartArea ca = new ChartArea("ChartArea1"); 
            ca.Area3DStyle.Enable3D = true;//開啟3D 
            this.mainChart.ChartAreas.Add(ca); 
            //Processor Legend 
            lgCPU = new Legend("Legend1"); 
            lgCPU.IsTextAutoFit = true; 
            lgCPU.Docking = Docking.Bottom; 
            this.mainChart.Legends.Add(lgCPU); 
            Series seCPU = new Series("SeriesCPU"); 
            seCPU.ChartArea = "ChartArea1"; 
            seCPU.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; 
            //seCPU.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar; 
            seCPU.IsVisibleInLegend = true; 
            seCPU.Legend = "Legend1"; 
            seCPU.LegendText = "CPU"; 
            seCPU.YValueMembers = "Processor"; 
            this.mainChart.Series.Add(seCPU); 
        } 
        PerformanceCounter pcCPU = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); 
        private void dispatcherTimer_Tick(object sender, EventArgs e) 
        { 
            if (dt.Rows.Count > 30)//這動作只是保留顯示30筆即可,不用一直再增加下去
            {
                dt.Rows.RemoveAt(0);
            }
            DataRow dr = dt.NewRow();

            dr["Processor"] = pcCPU.NextValue();//比例1:1
            lbl.Content = dr["Processor"];

            dt.Rows.Add(dr);
            //因為DataSource在Form Load就設了,所以這裡只要重新DataBind()就可以更新顯示資料,沒重DataBind之前,新資料不會顯示上去
            this.mainChart.DataBind();

            // Forcing the CommandManager to raise the RequerySuggested event
            CommandManager.InvalidateRequerySuggested();
        }
    }
}

cpu_display

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *