C # WinForm always detects your computer's CPU usage and physical memory usage

Source: Internet
Author: User

At first I thought I could use the trimer control, but only then knew that when the trimer was set to 100 microseconds, the entire form would be loaded and unusable. =

This use can only be used to load and delegate assignments using the loads.

CPU Usage
GetInfo GetInfo = new GetInfo (Userpercent,ram);
Thread th = new Thread (getinfo.threadprocsoft);
Th. Start ();

Defining delegate Types
delegate void Settextcallback (String str,string RAM);

<summary>
Event CPU usage for delegate execution
</summary>
public void Threadprocsoft ()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher ("SELECT * from Win32_Processor");
string usepre = null;
string rampre = null;
while (true)
{
foreach (ManagementObject myobject in searcher. Get ())
{
Usepre = myobject["Loadpercentage"]. ToString () + "%";
Lblcpu.text = myobject["Loadpercentage"]. ToString () + "%";
Mheight = Convert.ToInt32 (myobject["Loadpercentage"]. ToString ());
if (mheight = = 100)
Panel3. Height = 100;
CreateImage ();
Rampre = Memory () + "%";
}
This.settext (Usepre,rampre);
Thread.Sleep (300);
}
}

<summary>
Assign a value to the label of the CPU
</summary>
<param name= "str" ></param>
public void SetText (String str,string RAM)
{
if (lable1. invokerequired)
{
Resolve a "Access disposed handle" exception when the form is closed
while (Lable1. Ishandlecreated = = False)
{
if (lable1. Disposing | | Lable1. isdisposed) return;
}

Settextcallback d = new Settextcallback (setText);
Lable1. Invoke (d, new object[] {str,ram});
}
Else
{
Lable1. Text = str;
}
if (lable2. invokerequired)
{
Resolve a "Access disposed handle" exception when the form is closed
while (Lable2. Ishandlecreated = = False)
{
if (lable2. Disposing | | Lable1. isdisposed) return;
}

Settextcallback d = new Settextcallback (setText);
Lable2. Invoke (d, new object[] {str,ram});
}
Else
{
Lable2. Text = RAM;
}
}

C # WinForm always detects your computer's CPU usage and physical memory usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.