When deleting and creating performance counters in a service, it can hang the service

In the application I wrote, we delete and recreate performance counters as the application spins.

When you run this from the console, the application deletes and recreates the counters and counter class.

When we start the service, the application deletes the counter category as expected, but when a new category is created, it hangs until the service time

     if (PerformanceCounterCategory.Exists(CATEGORY_NAME))
     {
        PerformanceCounterCategory.Delete(CATEGORY_NAME);
     }

It works fine, but then freezes in this method

      private PerformanceCounterCategory RecreateTheCategory()
  {
     PerformanceCounterCategory category = null;
     if (!PerformanceCounterCategory.Exists(CATEGORY_NAME))
     {
        category = PerformanceCounterCategory.Create(CATEGORY_NAME, CATEGORY_HELP,
                                                     PerformanceCounterCategoryType.SingleInstance,
                                                     counterCreationDataCollection);
     }
     return category;
  }

in the line .Exists. I expect that this is related to permissions, but, as in the production process, we must start the service as a local system, I really need to fix it in the code without raising the user account

thanks

+3
1

, IOC OnStart()

+2

Source: https://habr.com/ru/post/1711245/


All Articles