none
Performance counter creation is very slow in a Windows Service after a machine reboot RRS feed

  • Question

  • I have a windows service which, in the OnStart method creates 4 performance counters as follows:

              string processName = Process.GetCurrentProcess().ProcessName;
                m_counterInfos = new List<CounterInfo>() {
                    new CounterInfo() { Name = @"Process\Processing", Unit = "%", Counter = new PerformanceCounter("Process", "% Processor Time", processName),
                        ConversionFactor = (1.0 / Environment.ProcessorCount), Format = "0.0", ErrorId = "11E59F44-2E43-4AEB-8135-5F828AA31E7A", WarningThreshold = 20.0, ErrorThreshold = 40.0 },
                    new CounterInfo() { Name = @"Process\Memory", Unit = "Mb", Counter = new PerformanceCounter("Process", "Working Set", processName),
                        ConversionFactor = 1.0 / (1024 * 1024), Format = "0", ErrorId = "A8647331-4467-428D-B029-9686BC1CE23E", WarningThreshold = 600.0, ErrorThreshold = 900.0 },
                    new CounterInfo() { Name = @"Process\Handles", Unit = "Handles", Counter = new PerformanceCounter("Process", "Handle Count", processName),
                        ConversionFactor = 1.0, Format = "0", ErrorId = "96718AE0-90ED-4835-957F-176AAFD424F6", WarningThreshold = 4000.0, ErrorThreshold = 8000.0 },
                        new CounterInfo() { Name = @"Process\Threads", Unit = "Threads", Counter = new PerformanceCounter("Process", "Thread Count", processName),
                        ConversionFactor = 1.0, Format = "0", ErrorId = "34DEC97A-DBA3-4652-A19D-C90D8056BCBD", WarningThreshold = 300.0, ErrorThreshold = 500.0  }
                };

    CounterInfo is a simple structure and so the overhead is in creating the performance counters.

    This section of code is taking around 150 seconds to run when the service is automatically started up after I restart the machine. If I restart just the service again this code takes around 2 seconds to run.

    How does it take so long on a system restart? I release that I could move this code out of the OnStart method so that they are created when required but I need to understand the problem before trying to work on the solution.

    Thanks

    Ian

    Tuesday, June 12, 2012 1:41 PM

All replies

  • Hi Ian,

    Welcome to the MSDN Forum.

    How do you measure the time period when it starts up when windows start?

    And do you have try it on another computer?

    Please try to test it on another computers to verify this is not related to your current computer.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 14, 2012 6:51 AM
    Moderator
  • Hi Ian, 

    Do you have any update?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 25, 2012 11:30 AM
    Moderator