C#實(shí)現(xiàn)顯示CPU使用率與內(nèi)存使用率
更新時(shí)間:2022年12月14日 15:18:00 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)顯示CPU使用率與內(nèi)存使用率,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下
實(shí)踐過程
效果

代碼
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();
//獲取物理內(nèi)存總量
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();
//獲取可用物理內(nèi)存總量
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();
//獲取虛擬內(nèi)存總量
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();
//獲取可用虛擬內(nèi)存總量
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);
//創(chuàng)建Graphics類對(duì)象
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;
}
}
partial class Form1
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.panel1 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.lblCPU = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lblMuse = new System.Windows.Forms.Label();
this.lblSum = new System.Windows.Forms.Label();
this.pbMemoryUse = new System.Windows.Forms.ProgressBar();
this.pbMemorySum = new System.Windows.Forms.ProgressBar();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.tsslNum = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel3 = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel4 = new System.Windows.Forms.ToolStripStatusLabel();
this.tssluse = new System.Windows.Forms.ToolStripStatusLabel();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.lblVuse = new System.Windows.Forms.Label();
this.lblVinfo = new System.Windows.Forms.Label();
this.pbVmemoryuse = new System.Windows.Forms.ProgressBar();
this.pbVmemorysum = new System.Windows.Forms.ProgressBar();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.panel1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.statusStrip1.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.panel1);
this.groupBox1.Location = new System.Drawing.Point(8, 7);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(89, 206);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "CPU 使用";
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.lblCPU);
this.panel1.Location = new System.Drawing.Point(7, 16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(74, 185);
this.panel1.TabIndex = 0;
//
// panel3
//
this.panel3.BackColor = System.Drawing.Color.Green;
this.panel3.Location = new System.Drawing.Point(26, 9);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(22, 154);
this.panel3.TabIndex = 4;
//
// lblCPU
//
this.lblCPU.AutoSize = true;
this.lblCPU.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblCPU.ForeColor = System.Drawing.Color.Lime;
this.lblCPU.Location = new System.Drawing.Point(24, 172);
this.lblCPU.Name = "lblCPU";
this.lblCPU.Size = new System.Drawing.Size(0, 12);
this.lblCPU.TabIndex = 3;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lblMuse);
this.groupBox2.Controls.Add(this.lblSum);
this.groupBox2.Controls.Add(this.pbMemoryUse);
this.groupBox2.Controls.Add(this.pbMemorySum);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(103, 7);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(321, 100);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "物理內(nèi)存(K)";
//
// lblMuse
//
this.lblMuse.AutoSize = true;
this.lblMuse.Location = new System.Drawing.Point(257, 61);
this.lblMuse.Name = "lblMuse";
this.lblMuse.Size = new System.Drawing.Size(41, 12);
this.lblMuse.TabIndex = 7;
this.lblMuse.Text = "label8";
//
// lblSum
//
this.lblSum.AutoSize = true;
this.lblSum.Location = new System.Drawing.Point(257, 39);
this.lblSum.Name = "lblSum";
this.lblSum.Size = new System.Drawing.Size(41, 12);
this.lblSum.TabIndex = 6;
this.lblSum.Text = "label7";
//
// pbMemoryUse
//
this.pbMemoryUse.Location = new System.Drawing.Point(69, 61);
this.pbMemoryUse.Name = "pbMemoryUse";
this.pbMemoryUse.Size = new System.Drawing.Size(183, 13);
this.pbMemoryUse.TabIndex = 4;
//
// pbMemorySum
//
this.pbMemorySum.Location = new System.Drawing.Point(69, 39);
this.pbMemorySum.Name = "pbMemorySum";
this.pbMemorySum.Size = new System.Drawing.Size(183, 13);
this.pbMemorySum.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(19, 61);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 1;
this.label2.Text = "可用數(shù):";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(31, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 0;
this.label1.Text = "總數(shù):";
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1,
this.tsslNum,
this.toolStripStatusLabel3,
this.toolStripStatusLabel4,
this.tssluse});
this.statusStrip1.Location = new System.Drawing.Point(0, 220);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(432, 22);
this.statusStrip1.TabIndex = 2;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(53, 17);
this.toolStripStatusLabel1.Text = "進(jìn)程數(shù):";
//
// tsslNum
//
this.tsslNum.AutoSize = false;
this.tsslNum.Name = "tsslNum";
this.tsslNum.Size = new System.Drawing.Size(50, 17);
this.tsslNum.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// toolStripStatusLabel3
//
this.toolStripStatusLabel3.Font = new System.Drawing.Font("宋體", 10F);
this.toolStripStatusLabel3.ForeColor = System.Drawing.Color.Gray;
this.toolStripStatusLabel3.Name = "toolStripStatusLabel3";
this.toolStripStatusLabel3.Size = new System.Drawing.Size(14, 17);
this.toolStripStatusLabel3.Text = "|";
//
// toolStripStatusLabel4
//
this.toolStripStatusLabel4.Name = "toolStripStatusLabel4";
this.toolStripStatusLabel4.Size = new System.Drawing.Size(65, 17);
this.toolStripStatusLabel4.Text = "CPU 使用:";
//
// tssluse
//
this.tssluse.Name = "tssluse";
this.tssluse.Size = new System.Drawing.Size(0, 17);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.lblVuse);
this.groupBox3.Controls.Add(this.lblVinfo);
this.groupBox3.Controls.Add(this.pbVmemoryuse);
this.groupBox3.Controls.Add(this.pbVmemorysum);
this.groupBox3.Controls.Add(this.label5);
this.groupBox3.Controls.Add(this.label6);
this.groupBox3.Location = new System.Drawing.Point(103, 113);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(321, 100);
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "虛擬內(nèi)存(K)";
//
// lblVuse
//
this.lblVuse.AutoSize = true;
this.lblVuse.Location = new System.Drawing.Point(257, 61);
this.lblVuse.Name = "lblVuse";
this.lblVuse.Size = new System.Drawing.Size(41, 12);
this.lblVuse.TabIndex = 9;
this.lblVuse.Text = "label8";
//
// lblVinfo
//
this.lblVinfo.AutoSize = true;
this.lblVinfo.Location = new System.Drawing.Point(257, 38);
this.lblVinfo.Name = "lblVinfo";
this.lblVinfo.Size = new System.Drawing.Size(41, 12);
this.lblVinfo.TabIndex = 8;
this.lblVinfo.Text = "label8";
//
// pbVmemoryuse
//
this.pbVmemoryuse.Location = new System.Drawing.Point(69, 60);
this.pbVmemoryuse.Name = "pbVmemoryuse";
this.pbVmemoryuse.Size = new System.Drawing.Size(183, 13);
this.pbVmemoryuse.TabIndex = 4;
//
// pbVmemorysum
//
this.pbVmemorysum.Location = new System.Drawing.Point(69, 38);
this.pbVmemorysum.Name = "pbVmemorysum";
this.pbVmemorysum.Size = new System.Drawing.Size(183, 13);
this.pbVmemorysum.TabIndex = 3;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(19, 60);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(53, 12);
this.label5.TabIndex = 1;
this.label5.Text = "可用數(shù):";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(31, 39);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(41, 12);
this.label6.TabIndex = 0;
this.label6.Text = "總數(shù):";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(432, 242);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "CPU使用率";
this.Load += new System.EventHandler(this.Form1_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.groupBox1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripStatusLabel tsslNum;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel3;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel4;
private System.Windows.Forms.ToolStripStatusLabel tssluse;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label lblCPU;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ProgressBar pbMemoryUse;
private System.Windows.Forms.ProgressBar pbMemorySum;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.ProgressBar pbVmemoryuse;
private System.Windows.Forms.ProgressBar pbVmemorysum;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label lblMuse;
private System.Windows.Forms.Label lblSum;
private System.Windows.Forms.Label lblVuse;
private System.Windows.Forms.Label lblVinfo;
}
到此這篇關(guān)于C#實(shí)現(xiàn)顯示CPU使用率與內(nèi)存使用率的文章就介紹到這了,更多相關(guān)C# CPU內(nèi)存使用率內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文帶你快速學(xué)會(huì)C#中WinForm框架的使用詳解
WinForm是一門非常經(jīng)濟(jì)實(shí)惠的技術(shù),就是說,可以在短時(shí)間內(nèi)學(xué)會(huì),并迅速借此進(jìn)行項(xiàng)目開發(fā)。本文就來和大家聊聊WinForm框架的使用方法,希望對(duì)大家有所幫助2023-02-02
C#實(shí)現(xiàn)自定義ListBox背景的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)自定義ListBox背景,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#根據(jù)http和ftp圖片地址獲取對(duì)應(yīng)圖片
這篇文章主要為大家詳細(xì)介紹了C#根據(jù)http和ftp圖片地址獲取對(duì)應(yīng)圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
時(shí)間字符串轉(zhuǎn)換成日期對(duì)象datetime的方法
在遇到形如"2012-12-19T17:00:00Z"這樣的時(shí)間字符串時(shí),怎樣轉(zhuǎn)換到DateTime類型呢,下面的方法可以解決2013-12-12

