Hello All,

I experience a strange problem I do not how to approach.
My windows service application uses several performance counter.
Before when staring the service deletes category and attempts to recreate
it.

*If* the category previously existed and has been deleted prior to
recreation an attempt to create a counter causes Service Manager timeout
after 30 sec. So, category gets deleted, but it can not be recreated.

I modified my service so it deletes category upon termination, but then it
fails during the restart while checking if the category exists.
It is completely weird.

Now, the really strange part: the same code executed as a console
application with *the same security credentials* executes momentarily.

Sample code attached below. The code uses .Net 2.0 methods but the same
issue can be seen in .Net 1.1.

Did anyone experience a similar issue? Is there any known solution?

Thank you,

Tomasz


Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

WriteToLog("Creating performance counters.")
PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")
WriteToLog("Performance counters created.")

RE: Strange Performance Counter performance problem in Windows Service application by v-kevy

v-kevy
Tue Oct 17 00:45:22 CDT 2006

Hi Tomasz,

I tried the same code on my machine in a windows service, the service works
fine. Could you let me know under which account as the service running?
Currently, I'm testing with Local System account. Also If you try to create
a new windows service and put these code in OnStart method. Does the new
service start successfully? (Please remove the log part to mitigate the
affect of logging.)

Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Re: Strange Performance Counter performance problem in Windows Service application by David

David
Tue Oct 17 04:56:34 CDT 2006

I had trouble with similar code on some server OS's . It was not consistent
in that different installations on different computers with the same OS
would behave differently. I don't think I was able to delete the counters,
let alone add new ones, but it's been years since it occurred and my memory
is hazy on some details.

The problem was that on some OS's the security settings on some registry
keys prevented any app from modifying the registry key. I had to modify the
ACL attached to the registry key before I could add performance counters.

The registry keys were at HKLM\Software\Microsoft\Windows
NT\CurrentVersion\Perflib\009. The values Counter and Help get modified
whenever a performance counter is added/removed from the system, and the
application must have Write access to these keys.You should also check the
values in the key immediately above this one, at
HKLM\Software\Microsoft\Windows NT\CurrentVersion\Perflib.

I never did find a workaround, I had to modify the ACL attached to the
registry key before I could write to it. I believe these settings were part
of some security lockdown.

"Tomasz Jastrzebski" <oegweb@nospam.nospam> wrote in message
news:ubC1n1V8GHA.2092@TK2MSFTNGP03.phx.gbl...
> Hello All,
>
> I experience a strange problem I do not how to approach.
> My windows service application uses several performance counter.
> Before when staring the service deletes category and attempts to recreate
> it.
>
> *If* the category previously existed and has been deleted prior to
> recreation an attempt to create a counter causes Service Manager timeout
> after 30 sec. So, category gets deleted, but it can not be recreated.
>
> I modified my service so it deletes category upon termination, but then it
> fails during the restart while checking if the category exists.
> It is completely weird.
>
> Now, the really strange part: the same code executed as a console
> application with *the same security credentials* executes momentarily.
>
> Sample code attached below. The code uses .Net 2.0 methods but the same
> issue can be seen in .Net 1.1.
>
> Did anyone experience a similar issue? Is there any known solution?
>
> Thank you,
>
> Tomasz
>
>
> Const ThisServiceName As String = "My Service"
>
> Trace.WriteLine("Checking performance counters category.")
> If PerformanceCounterCategory.Exists(ThisServiceName) Then
> Trace.WriteLine("Deleting performance counters.")
> PerformanceCounterCategory.Delete(ThisServiceName)
> Trace.WriteLine("Performance counters deleted.")
> End If
>
> WriteToLog("Creating performance counters.")
> PerformanceCounterCategory.Create(ThisServiceName, "",
> PerformanceCounterCategoryType.MultiInstance, "test counter", "")
> WriteToLog("Performance counters created.")
>



Re: Strange Performance Counter performance problem in Windows Service application by Tomasz

Tomasz
Tue Oct 17 13:21:41 CDT 2006

Hi David,

I do not think this is the problem.
I have forgotten to say that on every other attempt the service starts and
counters get created.
Under certain conditions it just takes forever to crate them.
There are *no errors* being thrown and it is quite inconsistent.
Also, I am part of local "Performance Log Users" and "Performance Monitor
Users" groups.

Thank you,

Tomasz

"David Levine" <SnipHereDlevinenntp2AndHere@wi.rr.com> wrote in message
news:emRgEKd8GHA.3760@TK2MSFTNGP02.phx.gbl...
>I had trouble with similar code on some server OS's . It was not consistent
>in that different installations on different computers with the same OS
>would behave differently. I don't think I was able to delete the counters,
>let alone add new ones, but it's been years since it occurred and my memory
>is hazy on some details.
>
> The problem was that on some OS's the security settings on some registry
> keys prevented any app from modifying the registry key. I had to modify
> the ACL attached to the registry key before I could add performance
> counters.
>
> The registry keys were at HKLM\Software\Microsoft\Windows
> NT\CurrentVersion\Perflib\009. The values Counter and Help get modified
> whenever a performance counter is added/removed from the system, and the
> application must have Write access to these keys.You should also check the
> values in the key immediately above this one, at
> HKLM\Software\Microsoft\Windows NT\CurrentVersion\Perflib.
>
> I never did find a workaround, I had to modify the ACL attached to the
> registry key before I could write to it. I believe these settings were
> part of some security lockdown.
>
> "Tomasz Jastrzebski" <oegweb@nospam.nospam> wrote in message
> news:ubC1n1V8GHA.2092@TK2MSFTNGP03.phx.gbl...
>> Hello All,
>>
>> I experience a strange problem I do not how to approach.
>> My windows service application uses several performance counter.
>> Before when staring the service deletes category and attempts to recreate
>> it.
>>
>> *If* the category previously existed and has been deleted prior to
>> recreation an attempt to create a counter causes Service Manager timeout
>> after 30 sec. So, category gets deleted, but it can not be recreated.
>>
>> I modified my service so it deletes category upon termination, but then
>> it fails during the restart while checking if the category exists.
>> It is completely weird.
>>
>> Now, the really strange part: the same code executed as a console
>> application with *the same security credentials* executes momentarily.
>>
>> Sample code attached below. The code uses .Net 2.0 methods but the same
>> issue can be seen in .Net 1.1.
>>
>> Did anyone experience a similar issue? Is there any known solution?
>>
>> Thank you,
>>
>> Tomasz
>>
>>
>> Const ThisServiceName As String = "My Service"
>>
>> Trace.WriteLine("Checking performance counters category.")
>> If PerformanceCounterCategory.Exists(ThisServiceName) Then
>> Trace.WriteLine("Deleting performance counters.")
>> PerformanceCounterCategory.Delete(ThisServiceName)
>> Trace.WriteLine("Performance counters deleted.")
>> End If
>>
>> WriteToLog("Creating performance counters.")
>> PerformanceCounterCategory.Create(ThisServiceName, "",
>> PerformanceCounterCategoryType.MultiInstance, "test counter", "")
>> WriteToLog("Performance counters created.")
>>
>
>



Re: Strange Performance Counter performance problem in Windows Service application by Tomasz

Tomasz
Tue Oct 17 14:38:57 CDT 2006

Hi Kevin,

You helped me to find a solution!
I have been trying to create counters within the Main() method.
The code, when executed in the OnStart() works just fine!
The remaining question is WHY???

Thank you,

Tomasz


"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> wrote in message
news:2A1C99a8GHA.4344@TK2MSFTNGXA01.phx.gbl...
> Hi Tomasz,
>
> I tried the same code on my machine in a windows service, the service
> works
> fine. Could you let me know under which account as the service running?
> Currently, I'm testing with Local System account. Also If you try to
> create
> a new windows service and put these code in OnStart method. Does the new
> service start successfully? (Please remove the log part to mitigate the
> affect of logging.)
>
> Const ThisServiceName As String = "My Service"
>
> Trace.WriteLine("Checking performance counters category.")
> If PerformanceCounterCategory.Exists(ThisServiceName) Then
> Trace.WriteLine("Deleting performance counters.")
> PerformanceCounterCategory.Delete(ThisServiceName)
> Trace.WriteLine("Performance counters deleted.")
> End If
>
> PerformanceCounterCategory.Create(ThisServiceName, "",
> PerformanceCounterCategoryType.MultiInstance, "test counter", "")
>
> Kevin Yu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
>
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>



Re: Strange Performance Counter performance problem in Windows Service application by v-kevy

v-kevy
Wed Oct 18 02:38:19 CDT 2006

Hi Tomasz,

I tried the same code in the Main() method, and it's working fine. Did you
put the code after System.ServiceProcess.ServiceBase.Run(ServicesToRun)? If
true, it might be the case that your code hangs. We should put it before
the service itself starts. My code that is working fine looks like the
follwing.

' The main entry point for the process
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
Service1, New MySecondUserService}
'
Const ThisServiceName As String = "My Service"

Trace.WriteLine("Checking performance counters category.")
If PerformanceCounterCategory.Exists(ThisServiceName) Then
Trace.WriteLine("Deleting performance counters.")
PerformanceCounterCategory.Delete(ThisServiceName)
Trace.WriteLine("Performance counters deleted.")
End If

PerformanceCounterCategory.Create(ThisServiceName, "",
PerformanceCounterCategoryType.MultiInstance, "test counter", "")

ServicesToRun = New System.ServiceProcess.ServiceBase() {New
Service1}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

Furthermore, it is not suggested to put your code in the main method. You
can put it in the OnStart method. And start your service in another thread
that does the main jobs.

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Re: Strange Performance Counter performance problem in Windows Service application by Tomasz

Tomasz
Wed Oct 18 12:18:47 CDT 2006

Hi Kevin,

No, I create counters *before*
"System.ServiceProcess.ServiceBase.Run(ServicesToRun)".
Moving call to a method creating counters to the OnStart() was the *only
change* I made. The behavior is/was very consistent.
I do not understand why, but it helped.

Tomasz

"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> wrote in message
news:pLRrpho8GHA.3352@TK2MSFTNGXA01.phx.gbl...
> Hi Tomasz,
>
> I tried the same code in the Main() method, and it's working fine. Did you
> put the code after System.ServiceProcess.ServiceBase.Run(ServicesToRun)?
> If
> true, it might be the case that your code hangs. We should put it before
> the service itself starts. My code that is working fine looks like the
> follwing.
>
> ' The main entry point for the process
> <MTAThread()> _
> <System.Diagnostics.DebuggerNonUserCode()> _
> Shared Sub Main()
> Dim ServicesToRun() As System.ServiceProcess.ServiceBase
>
> ' More than one NT Service may run within the same process. To add
> ' another service to this process, change the following line to
> ' create a second service object. For example,
> '
> ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New
> Service1, New MySecondUserService}
> '
> Const ThisServiceName As String = "My Service"
>
> Trace.WriteLine("Checking performance counters category.")
> If PerformanceCounterCategory.Exists(ThisServiceName) Then
> Trace.WriteLine("Deleting performance counters.")
> PerformanceCounterCategory.Delete(ThisServiceName)
> Trace.WriteLine("Performance counters deleted.")
> End If
>
> PerformanceCounterCategory.Create(ThisServiceName, "",
> PerformanceCounterCategoryType.MultiInstance, "test counter", "")
>
> ServicesToRun = New System.ServiceProcess.ServiceBase() {New
> Service1}
> System.ServiceProcess.ServiceBase.Run(ServicesToRun)
> End Sub
>
> Furthermore, it is not suggested to put your code in the main method. You
> can put it in the OnStart method. And start your service in another thread
> that does the main jobs.
>
> If anything is unclear, please feel free to let me know.
>
> Kevin Yu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
> ications.
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscriptions/support/default.aspx.
> ==================================================
>
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>



Re: Strange Performance Counter performance problem in Windows Service application by v-kevy

v-kevy
Wed Oct 18 22:16:37 CDT 2006

Hi Tomasz,

It seems to be a little complicated, since the same code works fine on my
machine both in OnStart and Main. There might be some other issues that
prevent Main from working properly. You can try to create a new windows
service and test the code again. Anyway, it was nice to know that the
solution works for you.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Re: Strange Performance Counter performance problem in Windows Service application by v-kevy

v-kevy
Mon Oct 23 03:35:33 CDT 2006

Hi Tomasz,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
Microsoft Online Community Support
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)