首页

C#实现 CPU内存使用率 功能源码

c#

2020-8-26

动态实时显示CPU 内存、当前进程数 等数据

    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;

        }
    }
资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect.sln , 920
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect.suo , 14336
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/bin/Debug/CPU_Detect.exe , 15360
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/bin/Debug/CPU_Detect.pdb , 36352
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/bin/Debug/CPU_Detect.vshost.exe , 14328
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/bin/Debug/CPU_Detect.vshost.exe.manifest , 490
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/CPU_Detect.csproj , 3864
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Form1.cs , 3853
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Form1.Designer.cs , 15711
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Form1.resx , 6209
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.csproj.FileListAbsolute.txt , 1966
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.csproj.GenerateResource.Cache , 910
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.exe , 15360
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.Form1.resources , 180
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.pdb , 36352
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/obj/Debug/CPU_Detect.Properties.Resources.resources , 180
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Program.cs , 491
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Properties/AssemblyInfo.cs , 1380
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Properties/Resources.Designer.cs , 2870
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Properties/Resources.resx , 5612
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Properties/Settings.Designer.cs , 1095
CPU─┌┤µ╩╣╙├┬╩/CPU_Detect/Properties/Settings.settings , 249
没有账号? 忘记密码?

社交账号快速登录