Advertisement
Guest User

New Code using Infinite Loop

a guest
Jan 29th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Management;
  11. using System.Timers;
  12.  
  13. namespace ezCPU
  14. {
  15.     public partial class ezCPU : Form
  16.     {
  17.  
  18.         public ezCPU()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         //On load, do this
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.             this.Text = this.Text + " - v" + Application.ProductVersion;
  27.             getCPUInfo();
  28.         }
  29.  
  30.         //Convert the string format to a readable double
  31.         public string convertClockSpeed(string s)
  32.         {
  33.             double clockSpeed = Convert.ToDouble(s);
  34.             double newClockSpeed = (clockSpeed / 1000);
  35.             return Math.Round(newClockSpeed, 2).ToString() + " GHz";
  36.         }
  37.  
  38.         public void getCPUInfo()
  39.         {
  40.             //Create the ManagementObjectSearcher grabbing everything from the Win32_Processor
  41.             ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");
  42.  
  43.             //Loop through the returned results
  44.             foreach (ManagementObject obj in myProcessorObject.Get())
  45.             {
  46.                 //Assign CPU Name
  47.                 string cpuName = obj["Name"].ToString();
  48.                 txtCPUName.Text = cpuName;
  49.  
  50.                 //Assign CPU Manufacturer
  51.                 string cpuManufacturer = obj["Manufacturer"].ToString();
  52.                 if (cpuManufacturer == "GenuineIntel")
  53.                 {
  54.                     txtCPUManufacturer.Text = "Genuine Intel";
  55.                 }
  56.                 else
  57.                 {
  58.                     txtCPUManufacturer.Text = cpuManufacturer;
  59.                 }
  60.  
  61.                 //Assign CPU cores
  62.                 string cpuCores = obj["NumberOfCores"].ToString();
  63.                 txtCores.Text = cpuCores;
  64.  
  65.                 //Assign CPU threads
  66.                 string cpuThreads = obj["ThreadCount"].ToString();
  67.                 txtThreads.Text = cpuThreads;
  68.  
  69.                 //Assign CPU max speed
  70.                 string cpuMaxSpeed = obj["MaxClockSpeed"].ToString();
  71.                 txtClockSpeed.Text = convertClockSpeed(cpuMaxSpeed);
  72.  
  73.                 updateCPUSpeed();
  74.             }
  75.         }
  76.  
  77.         private async Task updateCPUSpeed()
  78.         {
  79.             while (true)
  80.             {
  81.                 //Each tick, create a new searcher
  82.                 ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");
  83.  
  84.                 //For each searcher, assign the current CPU clock speed
  85.                 foreach (ManagementObject obj in myProcessorObject.Get())
  86.                 {
  87.                     //Display it
  88.                     string currentClockSpeed = obj["CurrentClockSpeed"].ToString();
  89.                     txtCPUSpeed.Text = convertClockSpeed(currentClockSpeed);
  90.                 }
  91.  
  92.                 await Task.Delay(10);
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement