My code:
public void CPUStats() { var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var ramCounter = new PerformanceCounter("Memory", "Available MBytes"); ramCounter.NextValue(); cpuCounter.NextValue(); System.Threading.Thread.Sleep(1000); string cpusage = cpuCounter.NextValue().ToString(); string ramusage = ramCounter.NextValue().ToString(); //Occurs here v try { //exception thrown here v cpuLabel.Invoke((MethodInvoker)(() => cpuLabel.Text = "CPU: " + cpusage.Remove(cpusage.IndexOf('.')) + "%")); } catch(ArgumentOutOfRangeException) { cpuLabel.Invoke((MethodInvoker)(() => cpuLabel.Text = "CPU: " + cpusage + "%")); } ramLabel.Invoke((MethodInvoker)(() => ramLabel.Text = "Free RAM: " + ramusage + "mb")); } At times my application will stop at the cpuLabel invoke and throw an "ArgumentOutOfRangeException" when it was handled and fixed in my code. I tried increasing the timer that activates the thread that activates CPUStats(), but to no avail.
Why would this happen?
catchblock generate exception?cpusagecontains a.and then take the appropriate action.