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 = =) Panel3. Height = 100; CreateImage (); Memory (); }} private void Memory () {Microsoft.VisualBasic.Devices.Computer MyInfo = new Microsoft. VisualBasic.Devices.Computer (); Get 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). ToString (); Gets the total amount of 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 (); Get total virtual Memory 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 (); Get 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) {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 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; } }}
CPU Memory utilization by C #