Performance counter throws error

S

sony.m.2007

Hi,
I'm using VSTS2008.
I tried to create a performance counter as below.

A simple example of monitoring processor usage (similar to what's seen
in task manager):



Code Snippet

using System;

using System.Diagnostics;

using System.Threading;



namespace PerfmonExample

{

class Program

{

static void Main(string[] args)

{

PerformanceCounter processorUsage = new
PerformanceCounter("Processor", "% Processor Time", "_Total",false);



while (true)

{

Console.WriteLine(processorUsage.NextValue());

Thread.Sleep(2000);

}

}

}

}


When i run the above code, it throws error as "The requested
Performance Counter is not a custom counter, it has to be initialized
as ReadOnly."
What could be the problem for this error?


Thanks,
Sony
 
P

Pavel Minaev

Hi,
I'm using VSTS2008.
I tried to create a performance counter as below.

A simple example of monitoring processor usage (similar to what's seen
in task manager):

Code Snippet

using System;

using System.Diagnostics;

using System.Threading;

namespace PerfmonExample

{

   class Program

   {

      static void Main(string[] args)

      {

         PerformanceCounter processorUsage = new
PerformanceCounter("Processor", "% Processor Time", "_Total",false);

         while (true)

         {

            Console.WriteLine(processorUsage.NextValue());

            Thread.Sleep(2000);

         }

      }

   }

}

When i run the above code, it throws  error  as "The requested
Performance Counter is not a custom counter, it has to be initialized
as ReadOnly."
What could be the problem for this error?

It is precisely what it says: when you called the constructor of
PerformanceCounter, you've passed "false" as the last argument, which
is named "readOnly". Try "true" instead.
 
S

sony.m.2007

Hi,
I'm using VSTS2008.
I tried to create a performance counter as below.
A simple example of monitoring processor usage (similar to what's seen
in task manager):
Code Snippet
using System;
using System.Diagnostics;
using System.Threading;
namespace PerfmonExample

   class Program
      static void Main(string[] args)
      {
         PerformanceCounter processorUsage = new
PerformanceCounter("Processor", "% Processor Time", "_Total",false);
         while (true)
         {
            Console.WriteLine(processorUsage.NextValue());
            Thread.Sleep(2000);
         }
      }
   }

When i run the above code, it throws  error  as "The requested
Performance Counter is not a custom counter, it has to be initialized
as ReadOnly."
What could be the problem for this error?

It is precisely what it says: when you called the constructor of
PerformanceCounter, you've passed "false" as the last argument, which
is named "readOnly". Try "true" instead.

Hi,
The same code working fin in VS2005.
I'm getting error while running the same code in VSTS 2008.
False means to access a counter in read/write mode.

Thanks,
Sony
 
P

Pavel Minaev

The same code working fin in VS2005.
I'm getting error while running the same code in VSTS 2008.
False means  to access a counter in read/write mode.

Yes, and you are not supposed to access the standard counters in read/
write mode; that's the whole point! That's what the error message
clearly says, too. Why do you think you can write to a "Processor
Time" system counter? What would that achieve, even if possible, apart
from skewing an important system statistic?
 
S

sony.m.2007

Yes, and you are not supposed to access the standard counters in read/
write mode; that's the whole point! That's what the error message
clearly says, too. Why do you think you can write to a "Processor
Time" system counter? What would that achieve, even if possible, apart
from skewing an important system statistic?

Hi,
Actually I'm trying to create my own custom performance counter for
database calls per second and I'm getting an error for that.
Is it possible to solve that error?

Thanks,
Sony
 
R

raylopez99

Hi,
Actually I'm trying to create my own custom performance counter for
database calls per second and I'm getting an error for that.
Is it possible to solve that error?

Thanks,
Sony

Overload the relevant method in System.Diagnostics at the library
level so that it conforms to your desires. But I personally have
issues with overloading any code written for the library--it's taboo,
but others might be more brave.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top