欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-21 16:10:07
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 新建一个窗体页面,设计布局如下 左边CPU使用是两个Panel,右边是PregressBar,下面是Status ......

场景

效果

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

 

 

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

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建一个窗体页面,设计布局如下

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

 

 

 

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

左边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