Jump to content

Can't figure out problem with code - Barnacules Codegasm #3

Tigeraceray

Hey guys I've been watching his series and I'm up to this video in the series but I hit a problem. I just for whatever reason cannot get my code to work. I've double checked everything he's doing and I have it all typed in right. Anyway here is the code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Diagnostics;using System.Threading;namespace Jarvis{    class Program    {        static void Main(string[] args)        {            //this will pull the cureent cpu load in percentage            PerformanceCounter perfCpuCount = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");            // This will pull the current available memory in Megabytes            PerformanceCounter perfMemCount = new PerformanceCounter("Memory", "Available MBytes");            //infinite while loop            while (true)            {                Thread.Sleep(1000);                Console.WriteLine("CPU Load:         {0}", perfCpuCount.NextValue());                Console.WriteLine("Available Memory: {0}", perfMemCount.NextValue());            }        }    }}

It should output the CPU load and available memory at this point but instead I get errors at lines 16 and 19 (bolded).

The error I get is:

Unhandled Exception: System.InvalidOperationException: Cannot load Counter Namedata because an invalid index '' was read from the registry.   at System.Diagnostics.PerformanceCounterLib.GetStringTable(Boolean isHelp)   at System.Diagnostics.PerformanceCounterLib.get_NameTable()   at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()   at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists)   at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)   at System.Diagnostics.PerformanceCounter.InitializeImpl()   at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)   at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName)   at Jarvis.Program.Main(String[] args) in c:\Users\XXXXXX\Documents\Visual Studio 2013\Projects\Jarvis\Program.cs:line 16Press any key to continue . . .

I have Windows 7 64 Bit Home Premium, he was doing it on Windows 8. Not sure if it makes a difference or not. Any ideas why its not working? It's driving me crazy at this point. Visual Studio isn't seeing any errors so I'm at a loss. Thanks guys!

 

Link to comment
Share on other sites

Link to post
Share on other sites

Try opening a command prompt and running the command "lodctr /r" then retry the program.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

It says it was unable to rebuild performance counter setting from system backup store, error code 5. I had a loss of power event a few weeks ago that nearly crippled the OS (still need to buy a UPS). I guess this is some damage from it.

Link to comment
Share on other sites

Link to post
Share on other sites

It says it was unable to rebuild performance counter setting from system backup store, error code 5. I had a loss of power event a few weeks ago that nearly crippled the OS (still need to buy a UPS). I guess this is some damage from it.

 

Make sure you're running it as an administrator and try capitalizing the /R

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

It says it was unable to rebuild performance counter setting from system backup store, error code 5. I had a loss of power event a few weeks ago that nearly crippled the OS (still need to buy a UPS). I guess this is some damage from it.

Just tried it. It cleared it up right away! Thanks Fizzle. Hopefully I won't have too many problems here on out with it. I had been stuck with that for the last few days.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×