Performance counter throws error

  • Thread starter Thread starter sony.m.2007
  • Start date Start date
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
 
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,
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
 
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?
 
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
 
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
 
Back
Top