C # CPU memory usage
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 [] MyProc Esses; 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 (); // obtain the total physical memory pbMemorySum. maximum = Convert. toInt32 (myInfo. info. totalPhysicalMemory/1024/1024); pbMemorySum. value = Convert. toInt32 (myInfo. info. totalPhysicalMemory/1024/1024); lblSum. text = (myInfo. info. totalPhysicalMemory/1024 ). to String (); // obtain the total available physical memory 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 (); // obtain the total virtual memory pbVmemorysum. maximum = Convert. toInt32 (myInfo. info. totalVirtualMemory/1024/1024); pbVmemorysum. value = Convert. toInt32 (myInfo. in Fo. totalVirtualMemory/1024/1024); lblVinfo. text = (myInfo. info. totalVirtualMemory/1024 ). toString (); // obtain the total available virtual memory 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) {checkforillegalcrossthreadcils = false; MyProcesses = Process. getProcesses (); tsslNum. text = MyProcesses. length. toString (); myUser ();} private void timerspontick (object sender, EventArgs e) {MyProcesses = Process. getProcesses (); tsslNum. text = MyProcesses. length. toString (); td = new Thread (new ThreadStart (myUser); td. start ();} private void form=formclosed (object sender, FormClos EdEventArgs 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 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 ;}}}