日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Winform中实现监控CPU内存使用率(附代码下载)

發布時間:2025/3/19 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform中实现监控CPU内存使用率(附代码下载) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

效果

?

注:

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

新建一個窗體頁面,設計布局如下

?

左邊CPU使用是兩個Panel,右邊是PregressBar,下面是StatusStrip,然后在頁面添加一個Timer,

然后修改其代碼如下

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.Diagnostics; using System.Threading; using System.Runtime.InteropServices; using System.Management; namespace CPU_Detect {public partial class Form1 : Form{public Form1(){InitializeComponent();}Process[] MyProcesses;Thread td;private void myUser(){ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");foreach (ManagementObject myobject in searcher.Get()){tssluse.Text = myobject["LoadPercentage"].ToString()+" %";lblCPU.Text = myobject["LoadPercentage"].ToString() + " %";mheight = Convert.ToInt32(myobject["LoadPercentage"].ToString());if (mheight == 100)panel3.Height = 100;CreateImage();Memory();}}private void Memory(){Microsoft.VisualBasic.Devices.Computer myInfo = new Microsoft.VisualBasic.Devices.Computer();//獲取物理內存總量pbMemorySum.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory/1024/1024);pbMemorySum.Value = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024);lblSum.Text = (myInfo.Info.TotalPhysicalMemory / 1024).ToString();//獲取可用物理內存總量pbMemoryUse.Maximum = Convert.ToInt32(myInfo.Info.TotalPhysicalMemory / 1024 / 1024);pbMemoryUse.Value = Convert.ToInt32(myInfo.Info.AvailablePhysicalMemory / 1024 / 1024);lblMuse.Text = (myInfo.Info.AvailablePhysicalMemory / 1024).ToString();//獲取虛擬內存總量pbVmemorysum.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);pbVmemorysum.Value = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);lblVinfo.Text = (myInfo.Info.TotalVirtualMemory / 1024).ToString();//獲取可用虛擬內存總量pbVmemoryuse.Maximum = Convert.ToInt32(myInfo.Info.TotalVirtualMemory / 1024 / 1024);pbVmemoryuse.Value = Convert.ToInt32(myInfo.Info.AvailableVirtualMemory/ 1024 / 1024);lblVuse.Text = (myInfo.Info.AvailableVirtualMemory / 1024).ToString();}private void Form1_Load(object sender, EventArgs e){CheckForIllegalCrossThreadCalls = false;MyProcesses = Process.GetProcesses();tsslNum.Text = MyProcesses.Length.ToString();myUser();}private void timer1_Tick(object sender, EventArgs e){MyProcesses = Process.GetProcesses();tsslNum.Text = MyProcesses.Length.ToString();td = new Thread(new ThreadStart(myUser));td.Start();}private void Form1_FormClosed(object sender, FormClosedEventArgs e){if (td != null){td.Abort();}}int mheight = 0;private void CreateImage(){int i=panel3.Height/100;Bitmap image = new Bitmap(panel3.Width,panel3.Height);//創建Graphics類對象Graphics g = Graphics.FromImage(image);g.Clear(Color.Green);SolidBrush mybrush = new SolidBrush(Color.Lime);g.FillRectangle(mybrush,0,panel3.Height-mheight*i,26,mheight*i);panel3.BackgroundImage = image;}} }

代碼下載

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/12243741

總結

以上是生活随笔為你收集整理的Winform中实现监控CPU内存使用率(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。