NextValue() for PerformanceCounter() produces Win32Exception. RRS feed

  • Question

  • Hello C# Developers:

    I need some help.   I am trying to use PerformanceCounter().   These are the 5 lines of code in question:

    PerformanceCounter pcCPUUsage = new PerformanceCounter();
    pcCPUUsage.CategoryName = "Processor";      // Example code at http://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c
    pcCPUUsage.CounterName = "% Processor Time"; //2nd Example at http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html
    pcCPUUsage.InstanceName = "_Total";
    string strNext = pcCPUUsage.NextValue().ToString();

    The code contains two URLs where this code is given as working examples.   The code in fact works on two of my computers, but not my main development computer.   The code Exceptions out on the last line of code on the NextValue() method, where a Win32Exception was unhandled.   The explanation given is "The service cannot be started, either becaue it is disabled or because it has no enabled devices associated with it."

    If the problem is in fact that a service can't be started, could you please identify the NAME of the service so that I can attempt to start that service?   I've looked all over the Internet trying to find the name of the service that PerformanceCounter() might be dependant on, and I can't find it.  The class mentions the Win32Exception, but does not say anything on how to deal with it, let alone mentioning that a service cannot be started.

    The computer where this issue occurs is using Windows XP Pro, fully patched up to today.   (I have gotten this to work on two other computers, one using Windows XP Pro and Windows 7, but the lack of a problem on these two machines has not helped me to determine the identity of the service that apparently can't be started on my Windows XP Pro development computer.)   The code should return the same exact CPU Usage percent that we commonly see in the Performance tab of the Windows Task Manager.   Windows Task Manager does work on this computer, and I have been able to run PerfMon.exe on the computer, although I must admit not to know how to accomplish anything with it.

    A response and answer would be deeply appreciated.   Thank you in advance for your reply.

    Sincerely,  Burton G. Wilkins

    .Net Developer
    • Moved by Leo Liu - MSFT Friday, September 9, 2011 9:38 AM Moved for better support. (From:Visual C# General)
    Thursday, September 8, 2011 2:52 AM

All replies

  • Hi, i spent a couple of time of research about this and have a look at this site.

    Failure is not the worst thing in the world. The very worst is not to try. Email Address : ericjohnadamos@gmail.com. http://ericjohnadamos.blogspot.com/
    Thursday, September 8, 2011 3:30 AM

  • Try calling InitializeLifeTimeService() before getting the next value. Like below,

    PerformanceCounter pcCPUUsage = new PerformanceCounter();
    pcCPUUsage.CategoryName = "Processor";
    pcCPUUsage.CounterName = "% Processor Time";
    pcCPUUsage.InstanceName = "_Total";
    string strNext = Convert.ToString(pcCPUUsage.NextValue());


    Please mark this post as answer if it solved your problem. Happy Programming!
    Thursday, September 8, 2011 5:23 AM
  • Dear Eric:

    I took your suggestion, and looked at this site (http://poorperformance.com/wiki/index.php?title=How_to_monitor_a_Process/Service/Performance_Counter_on_a_Remote/Different_Computer);  Unfortunately, the points made on the site made no difference.   The error message remains the same "Win32Exception":  "The service cannot be started, either becaue it is disabled or because it has no enabled devices associated with it."

    Let me share with you each of these points, and give you my response:  SUGGESTION Step 1:  The "remote registyry service" must be running on the remote machine.  RESPONSE 1:  The program is not running on a remote/different computer so I don't think the article's points are possibly relivant.   Being hopeful, I went through each point hoping that maybe the article would be relavent (but it wasn't).   I went ahead and checked, and found that on my development computer, this service was disable.  So I then enabled, set it for automatic startup, and then started the service.   The error remained constant when I retried those lines of code.  I then tried booting the machine, and still the error message remained constant.   I then went and checked on the two computers where these 5 lines of code had run successfully.  On the Windows 7 computer, the Remote Registry service was set to Manual and was not running.   Still the five lines of code processed without an exception.   I then checked the other Windows XP computer.  On this 2nd XP machine, the Remote Registry service was running.   It appears this specific service has no relivance to the problem because the progam is being run locally, and is not being run on a differant computer.

    Step 2:  You must have at least Read access to these two registry keys on the remote computer.   I checked both registry keys

  • HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg  and
  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib

    ResponsAll users on the computer where these 5 lines of code were failing had at least Read access to these keys.

    Have you become aware of the name of the service that would cause the raising of this Exception's error message?

    Anyway, thank you for your assistance.   If you find something that would solve this error, please get back to me.


    Burton G. Wilkins



  • Burton G. Wilkins, .Net Developer
Thursday, September 8, 2011 7:01 AM
  • Dear Adavesh:

    Thank you for your suggestion,  I added these two lines of code that you suggested, and used the Convert function in creating the string response.

    pcCPUUsage.CategoryName =

    "Processor"; //http://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-cpcCPUUsage.CounterName = "% Processor Time"; //http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html
    pcCPUUsage.InstanceName = "_Total";



    string strNext = Convert.ToString(pcCPUUsage.NextValue());

     Unfortunately, none of these changes solved the Excepting out of these 5 lines of code.  The problem still exists wioutl improvement whatsoever.

    I mentioned in my request for assistance for the identity of the service - if any - that the error message was claiming could not be started.  Do you know of the name of the service this error message is complaining about?

    Thank you for your assistnce.  If you have any additional thoughts taht might resolve this issue, please get back to me with that information.


    Burton G. Wilkins,

    • Edited by Burt Wilkins Thursday, September 8, 2011 7:31 AM Server seized the first time I tried to submit this response
    Thursday, September 8, 2011 7:13 AM
  • Dear Friends:

    I have had NO workable solution proposed in this whatsoever.   Does anyone have an idea how to deal with this Win32Exception exception?

    Look, I am concerned about even using PerformanceCounters() at all.   I am writing a program which I think could have high commercial value.   The program needs data to draw conclusions, and actually do something.   Yet, what I read of PerformanceCounters(), the registry values used are not reliable.   The only way to fix them - according to Microsoft - is by manually fixing the Register (http://support.microsoft.com/kb/300956).   Very dangerous.   And here you have PerformanceCounters() failing on Win32Exception.   I too have gone through the Internet looking for information on PerformanceCounters().   There appears to be NOT ONE WORD on this exception.   The two gentlemen who attempted responses to this inquiry, could not answer one word in response to the error message this PerformanceCounter() has given.  From such responses, is the implicaton that the error message itself from Microsoft is totally unrealiable and to be ignored, and that indeed no one on this planet has the slightest idea of how to address this Win32Exception?

    Some might say, "Well if your unsatisfied, call Microsoft and spend money."  But look at the issue more in terms of a deliverable product.   When this fails elsewhere are the users then to call Microsoft?   Are technicians then to work with each one of these computers one computer at a time simply to fix them?   If these PerformanceCounters() are so poorly supported, would not their failure (so far 1 in 4) reflect upon the product?  How many failures does it take to kill a product????

    I am concerned about creating a maintainable program.   Not one where a feature works on 3 out of 4 computers as this one has.   The feature has to work 4 out of 4 computers, and if there is a problem then there should be clear fixes that users in the field not just a programmer can easily implement.   That being the case, how then could any program responsibly use PerformanceCounters() if there is not even a word anywhere as to how to deal with this problem?   How foolish could one be to use a feature where when they fail, user's and administrators are then told that they must manually  make entries to the Registry, and in this case where there is not a word to explain dealing with other types of errors such as this Win32Exception?  Has Microsoft in this case produced simply crab?

    Certainly as professional programmers, this should concerns you.   The features of this language should be professionally supported.   Yet here, I ask you how do PerformanceCounters() meet that standard at all?


    Burton G. Wilkins, Programmer.


    Burton G. Wilkins, .Net Developer
    Thursday, September 8, 2011 11:57 PM