C# 任務管理器之cpu利用率

先上效果圖:
在這裏插入圖片描述說一個遇到的bug:
在這裏插入圖片描述這種情況是關閉了主窗口,但正在運行的線程沒有被關閉。

解決方法:
把該線程的IsBackground設爲true;
即把該線程設置爲後臺線程。

代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace 控制管理其
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        PerformanceCounter pf = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        List<Line> list = new List<Line>();
        int sum = 0;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Thread th = new Thread(run);
            th.Start();
            th.IsBackground = true;
        }
        public void run()
        {
            
            while(true)
            {
                double f = pf.NextValue();
                App.Current.Dispatcher.Invoke(() =>
                {
                    canvas.Children.Clear();
                    for (int i = 0; i < 30; i++)
                    {
                        Line l = new Line();
                        l.Stroke = new SolidColorBrush(Colors.Green);
                        l.StrokeThickness = 1;
                        l.Y1 = 0;
                        l.Y2 = 300;
                        l.X1 = 15 * i;
                        l.X2 = 15 * i;
                        canvas.Children.Add(l);
                    }
                    for (int i = 0; i < 21; i++)
                    {
                        Line l = new Line();
                        l.Stroke = new SolidColorBrush(Colors.Green);
                        l.StrokeThickness = 1;
                        l.X1 = 0; l.X2 = 435;
                        l.Y1 = i * 15; l.Y2 = i * 15;
                        canvas.Children.Add(l);
                    }
                    //  MessageBox.Show(list.Count.ToString());
                    if (list.Count == 0)
                    {
                        Line l = new Line();
                        l.Stroke = new SolidColorBrush(Colors.LightGreen);
                        l.X1 = 435; l.X2 = 435;
                        l.Y1 = 300 - f * 3;
                        l.Y2 = 300 - f * 3;
                        list.Add(l);
                    }
                    else
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            list[i].X1 -= 15; list[i].X2 -= 15;
                            if (list[i].X1 < 0)
                            {
                                list.RemoveAt(i);
                                i = -1;
                            }
                        }
                        Line l = new Line();
                        l.X1 = list[list.Count - 1].X2;
                        l.Y1 = list[list.Count - 1].Y2;
                        l.X2 = 435; l.Y2 = 300 - f * 3;
                        l.Stroke = new SolidColorBrush(Colors.LightGreen);
                        l.StrokeThickness = 1;
                        list.Add(l);
                    }
                    for (int i = 0; i < list.Count(); i++)
                        canvas.Children.Add(list[i]);
                });
                Thread.Sleep(1000);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章