Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:21:53+00:00 2026-05-28T14:21:53+00:00

I am creating and setting up the performance counters correctly but when I delete

  • 0

I am creating and setting up the performance counters correctly but when I delete the category, recreate the category with the same name and add/update the counters to that category, it fails to update the counters and its values.

The following code runs fine for the first time but not the second time. The code to remove the “Delete category” is not needed right now but I want to be able to delete existing category each time we deploy our application.

How can I permanently delete the counter if its not doing so or reset its values?

    private PerformanceCounter mainCounter;
    private PerformanceCounter mainCounterBase;
    private string category = "TestPerformanceCounterTest";
    public void Test()
    {
                   //Counter setup

        if (PerformanceCounterCategory.Exists(category))
            PerformanceCounterCategory.Delete(category);
        if (!PerformanceCounterCategory.Exists(category))
        {
            var categoryCollection = new CounterCreationDataCollection();

            var counter1 = new CounterCreationData("RawCounter1", "", PerformanceCounterType.RawFraction);
            var counter2 = new CounterCreationData("RawCounterBase1", "", PerformanceCounterType.RawBase);
            categoryCollection.Add(counter1);
            categoryCollection.Add(counter2);


            PerformanceCounterCategory.Create(category, "", PerformanceCounterCategoryType.SingleInstance, categoryCollection);

            //  Wait and wait...
            Thread.Sleep(TimeSpan.FromSeconds(3));
        }
                    //create counters
                    mainCounter = new PerformanceCounter(category, "RawCounter1", false);
        mainCounterBase = new PerformanceCounter(category, "RawCounterBase1", false);
                    //reset values
                    mainCounter.RawValue = 0;
        mainCounterBase.RawValue = 0;

                    //update counter
                    mainCounter.IncrementBy(10);
        mainCounterBase.IncrementBy(20);
        **Console.WriteLine("Main counter: " +mainCounter.RawValue);//doesnt show value 50 the second time this is run**
        Console.WriteLine("Main counter Base: " + mainCounterBase.RawValue);
        Console.WriteLine("Main counter next value: " + mainCounter.NextValue());
        Console.WriteLine("Main counter base next value: " + mainCounterBase.NextValue());
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:21:53+00:00Added an answer on May 28, 2026 at 2:21 pm

    I am pretty sure this is due to the way Windows manages performance data.

    From MSDN, PerformanceCounterCategory.Create Method (String, String, PerformanceCounterCategoryType, CounterCreationDataCollection):

    Note
    It is strongly recommended that new performance counter
    categories be created during the installation of the application, not
    during the execution of the application. This allows time for the
    operating system to refresh its list of registered performance counter
    categories. If the list has not been refreshed, the attempt to use the
    category will fail.

    I don’t have firsthand knowledge, but this suggests that the addition or deletion of categories is not a synchronous action.

    To work around this, you might want to replace your first if with a while instead, like so:

    while (PerformanceCounterCategory.Exists(category))
    {
        PerformanceCounterCategory.Delete(category);
    }
    

    That’s a little heavy-handed, though. Best recommendation is to not do the counter set up or tear down just before you need it. Instead, put it into an installer, or at the very least, create a separate tool to install/uninstall them. Also, you could create a Powershell script to install/uninstall them. See http://msdn.microsoft.com/en-us/library/windowsazure/hh508994.aspx for an example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a JSpinner and setting a NumberEditor with a custom format. But no
I'm creating an HttpCookie, setting only the name and value and not the expires
When creating a view and setting group by value some field, it groups only
I am creating an NSURLMutableRequest and setting HTTP methods and content-types and such. All
I'm creating a GroupBox at runtime and setting its Text property. Usually, the text
I am having trouble with setting up the open source library JFreeChart for creating
I'm printing a document by creating a System.Diagnostics.Process object and then setting the StartInfo
Below I am creating an object in JavaScript. Within the constructor I am setting
I am creating a non-deterministic orchestration to handle convoys. I know I am setting
I am creating a DynamicMultiMock as follows: this.serviceClient = this.mocks.DynamicMultiMock<ISlippyPlateProcedureService>(typeof(ICommunicationObject)); Then setting the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.