Form is freezing when using timer
Hi all..
I am having some problems with my code.
I need a textbox to update the CPU current speed. Only problem is that
the form itself freezes when I use a normal timer.
The timer updates every 500 ms...
How else should I attack this problem?
//Code
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.Management;
using Microsoft.Win32;
namespace HardwareMonitor
{
public partial class SystemMonitor : Form
{
ManagementObject MO = new ManagementObject("Win32_processor.DeviceID='CPU0'");
delegate void DelegateUpdate();
public SystemMonitor()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//CPU Name
string CPUName = (string)(MO["Name"]);
txtCPUName.Text = CPUName;
}
private void timer1_Tick(object sender, EventArgs e)
{
DelegateUpdate delegat = new DelegateUpdate ( () =>
{
//CPU speed using WMI. Win32_processor.DeviceID
uint speed = (uint)(MO["CurrentClockSpeed"]);
txtCurrentSpeed.Text = Convert.ToString(speed) + " MHz";
//CPU Core Voltage
UInt16 CurrentVoltageRaw = (UInt16)(MO["CurrentVoltage"]);
//Devide the voltage by 10 to get the real value
double CurrentVoltage = Convert.ToDouble(CurrentVoltageRaw);
CurrentVoltage /= 10;
txtCoreVoltage.Text = Convert.ToString(CurrentVoltage) + " V";
MO.Dispose();
});
txtCoreVoltage.Invoke(delegat);
txtCurrentSpeed.Invoke(delegat);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
Close();
}
private void btnCheck_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}