Chris Harris
Chris Harris

Reputation: 21

Asynchronous Call to Timer

I am trying to create an application that shows the system statistics but it also executes a timer each second to update the current CPU clock speed. I have tried numerous solutions but none seem to work for me. I will attach my code below. I am using the System.Management DLL to call the stats I am wanting. Thank you!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace ezCPU
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = this.Text + " - v" + Application.ProductVersion;
            getCPUInfo();
        }

        public string convertClockSpeed(string s)
        {
            double clockSpeed = Convert.ToDouble(s);
            double newClockSpeed = (clockSpeed / 1000);
            return Math.Round(newClockSpeed, 2).ToString() + " GHz";
        }

        public void getCPUInfo()
        {
            ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");

            foreach (ManagementObject obj in myProcessorObject.Get())
            {
                string cpuName = obj["Name"].ToString();
                txtCPUName.Text = cpuName;

                string cpuManufacturer = obj["Manufacturer"].ToString();
                if (cpuManufacturer == "GenuineIntel")
                {
                    txtCPUManufacturer.Text = "Genuine Intel";
                }
                else
                {
                    txtCPUManufacturer.Text = cpuManufacturer;
                }

                string cpuCores = obj["NumberOfCores"].ToString();
                txtCores.Text = cpuCores;

                string cpuThreads = obj["ThreadCount"].ToString();
                txtThreads.Text = cpuThreads;

                string cpuMaxSpeed = obj["MaxClockSpeed"].ToString();
                txtClockSpeed.Text = convertClockSpeed(cpuMaxSpeed);
            }
        }  

        private void cpuTimer_Tick(object sender, EventArgs e)
        {
            ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");

            foreach(ManagementObject obj in myProcessorObject.Get())
            {
                string currentClockSpeed = obj["CurrentClockSpeed"].ToString();
                txtCPUSpeed.Text = convertClockSpeed(currentClockSpeed);
            }  
        } 
    }
}

This is the result of the code. However, I am unable to move the form around while it is still updating.

https://i.gyazo.com/5770d7a0eabcf5f648537a7d9ebc0bff.gif

Upvotes: 1

Views: 117

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

Have you tried the windows forms timer? It has the elapsed event that even spins up a background thread to do the work, keeping your ui responsive:

https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.elapsed?redirectedfrom=MSDN&view=netframework-4.8


public class Form1: Form
{
    public System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();

    public Form1()
    {
         InitializeComponent();

        // Create a timer and set a two second interval.
        aTimer.Interval = 2000;

        // Hook up the tick event for the timer. 
        aTimer.Tick += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private void OnTimedEvent(object sender, EventArgs e)
    {
        // update your statistics here
    }
}

If you want to update the ui there, then the tick event is the way to go: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer.tick?view=netframework-4.8

Upvotes: 1

Related Questions