Cannot create performance counters if Perfmon is open

G

Guest

We are having problems instantiating performance counters if Perfmon is open. We get the error:

"System.InvalidOperationException: Cannot create file mapping.
at System.Diagnostics.FileMapping.Initialize()
at System.Diagnostics.FileMapping..ctor()
at System.Diagnostics.SharedPerformanceCounter.get_FileView()
at System.Diagnostics.SharedPerformanceCounter.ResolveOffset(Int32 offset)
at System.Diagnostics.SharedPerformanceCounter.GetCounter(String
categoryName, String counterName, String instanceName)
at System.Diagnostics.SharedPerformanceCounter..ctor(String categoryName,
String counterName, String instanceName)
at System.Diagnostics.PerformanceCounter.Initialize()
at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String
counterName, String instanceName, Boolean readOnly)"

What's the deal? And don't say, "Then make sure perfmon is closed when you make the call.", because we have perfmon monitoring our services all day long. We can't stop monitoring just so we can start and stop one of our services.
 
Y

Ying-Shen Yu[MSFT]

Hi,

I reviewed your post carefully, also wrote some test code to try to
reproduce your problem, however the test code works fine on my system, I
think more information on this issue is needed before we assist you
investigating it further.

I'd like to know which version of .NET Framework and OS you are using, what
performance counters is used in your program and if they are opened in
ReadOnly mode?

Looking forward to your reply!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Y

Ying-Shen Yu[MSFT]

Hi,

I tried to reproduce your problem on a W2k machine with .NET v1.0, but I
could not repro this problem on my side.
Here is the detail reproduce steps I taken:
I used the sample in .NET Framework quick start to create a Performance
counter and write value into it.
Then I opened perfmon to monitor this counter.
I use another console app to create this performance counter in read write
mode and retrieve value from it.

I'd like to get more information on this problem, if it is possible to
build a small sample to reproduce this problem, please send it to me by
mail, I'll take a look into it.
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Y

Ying-Shen Yu[MSFT]

Hi,

I strictly followed your steps to try to reproduce this problem, however
the problem still could not be reproduced on my system.
Here is my test code, could you check it and let me know the difference,
also you may test it on your system to see if the problem could be
reproduced using this test.

Thank you for your patience!

using System;
using System.Diagnostics;
using System.Threading;

public class PCWrite
{

public static void Main(string[] args)
{
string objectName = "MyObj";
string counterName = "MyCounter";
string instanceName = "MyInstance";

if(!PerformanceCounterCategory.Exists(objectName))
{
PerformanceCounterCategory.Create(objectName,"Simple Counter
Object",counterName,"Simple Counter");
}

Console.WriteLine("Category Exists");

if(!PerformanceCounterCategory.CounterExists(counterName, objectName))
{
Console.WriteLine("The counter does not exists!");
return;
}

Console.WriteLine("Counter Exists");

PerformanceCounter aCounter = new PerformanceCounter(objectName,
counterName ,instanceName, false);
aCounter.RawValue = 50;

Console.WriteLine("Press \'q\' to quit the sample");
Console.WriteLine("Press \'+\' to increment the counter");
Console.WriteLine("Press \'-\' to decrement the counter");

Console.WriteLine("Started");
int command;
do
{
command = Console.Read();
if(command=='-') aCounter.IncrementBy(-5);
if(command=='+') aCounter.IncrementBy(5);
}
while(command!='q');
}
}


Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Y

Ying-Shen Yu[MSFT]

Hi,

I modified the test code to use a timer to update the value, the problem
still not occurred.
I also tried using a tight loop to update the value and it works well too.

One thing I noticed is you need call RemoveInstance method when you
stopping your service, or Performance Monitor will still hold the instance,
which might cause another InvalidOperationException (The peration couldn't
be completed, potential internal deadlock). You may try adding this method
call if you didn't use it before.

Here is my modified test code, to further assist you investigate this
problem, I think a reproduce sample is needed. If you could make a simple
sample to reproduce this issue, please send it to me.
<code>
using System;
using System.Diagnostics;
using System.Threading;

public class PCWrite
{
private static PerformanceCounter aCounter;

public static void Main(string[] args)
{
string objectName = "MyObj";
string counterName = "MyCounter";
string instanceName = "MyInstance";
if(!PerformanceCounterCategory.Exists(objectName))
{
PerformanceCounterCategory.Create(objectName,"Simple Counter
Object",counterName,"Simple Counter");
}

Console.WriteLine("Category Exists");

if(!PerformanceCounterCategory.CounterExists(counterName, objectName))
{
Console.WriteLine("The counter does not exists!");
return;
}

Console.WriteLine("Counter Exists");

aCounter = new PerformanceCounter(objectName, counterName ,instanceName,
false);
aCounter.RawValue = 50;

System.Timers.Timer timer = new System.Timers.Timer(5);
timer.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
Console.WriteLine("Started");
Console.ReadLine();
aCounter.RemoveInstance();
}

private static void timer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
aCounter.IncrementBy(5);
Console.WriteLine("timer");
}
}
</code>

Thanks!



Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 

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