views:

143

answers:

3

I have a C# application that adds some performance counters when it starts up. But if the registry HKEY_LOCAL_MACHINE->SOFTWARE->Microsoft->Windows NT->CurrentVersion->Perflib is corrupted (missing or invalid data), the operation of checking the existence of the performance counters (PerformanceCounterCategory.Exists(category) takes a really long time (around 30 secs) before finally throwing exception (InvalidOperation: Category does not exist).

My question is how can i verify the validity of the registry before trying to add the performance counters (and what validity means) or if there is a way i can timeout the perf counter operations, so that it doesn't take 30 seconds to get an exception.

+1  A: 

I suspect that "validity" is an internal implementation detail that you don't get to know. However, you could at least try to open the same registry keys and just see if they exist. You can use Process Explorer to figure out which keys it is reading.

However, I question why you would even care. A corrupted registry shouldn't be a very common thing, and if it is, what are you going to do about it? All you can do is quit. So you might as well just catch the exception. I would treat this like any other blocking operation and do it on a worker thread (not the UI thread) and show progress to your users so they know your app isn't hung.

jeffamaphone
I care because it affects the user experience of the product, and while i agree with you that invalid registry entries should not be a common occurrence, I still have to get this scenario covered. I asked the question hoping that someone else hit this roadblock and had a more concrete solution. Thanks anyway
anchandra
Yeah, I think the best you can do is not hang and handle it, rather than try to predict it will happen. Maybe there's a better way though.
jeffamaphone
A: 

Are you sure the registry is corrupt?

I found that my program did not have permission to create performance counters at run time. Instead, I added the creation of the counters to my installer program, and the installer must be run as an administrator. At run time my program had no problems accessing or updating the already created counters.

aaaa bbbb
It is not a problem of permissions, since i already have a check in place to ensure that the user has admin rights. The entries in the registries have no value, thus throwing exception after 30 secs. In fact, i do not care if i get an exception, as long as it doesn't take 30 secs to get it
anchandra
A: 

I don't know how to answer your question as asked, but if I understand correctly, the problem is that it looks to the user as if the application hangs for up to 30 seconds on startup.

If that is the case, I'd suggest that you might be able to avoid that by just creating a worker thread, telling it to create the performance monitors and then carrying on with starting the application.
I've not worked with performance counters so I can't say if there's anything with them that would stop this from working?

ho1