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 8372645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:29:08+00:00 2026-06-09T14:29:08+00:00

I am using this class as a base class for a category of tests

  • 0

I am using this class as a base class for a category of tests that launch a process and give it some input and wait for it to become idle before giving it more input.

public abstract class TestProcessLaunchingBase
{
    protected PerformanceCounter PerfCounter { get; set; }

    protected void WaitForProcessIdle()
    {
        while (true)
        {
            float oldValue = PerfCounter.NextValue();

            Thread.Sleep(1000);

            float nextValue = PerfCounter.NextValue();

            if (nextValue == 0)
                break;
        }
    }

    protected void FindSpawnedProcessPerfCounter(int processId)
    {
        PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
        string[] instances = cat.GetInstanceNames();
        foreach (string instance in instances)
        {
            using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
            {
                int val = (int)cnt.RawValue;
                if (val == processId)
                {
                    PerfCounter = new PerformanceCounter("Process", "% Processor Time", instance);
                    break;
                }
            }

        }

        Assert.IsNotNull(PerfCounter, "Failed to perf counter");
    }
}

These tests occasionally fail because PerfCounter.NextValue() throws an

System.InvalidOperationException
Instance ‘foobar#2’ does not exist in the specified Category

It seems like the instance name of the performance counter is not persistent.

If there are three foobar processes they might have instance names

  • foobar pid 5331
  • foobar #1 pid 5332
  • foobar #2 pid 5333

It seems like if pid 5332 exits foobar #2 becomes foobar #1.

Questions:

  1. Is this a documented behavior ? Can you not persistent a performance counter ? Do you have to look it up every time ?

  2. Alternatively, is there a performance counter that can give Processor Time for all processes named foobar

  • 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-06-09T14:29:11+00:00Added an answer on June 9, 2026 at 2:29 pm

    I already faced this issue in the past. The ProcessName#InstanceNumber pattern for the instance name was clearly a poor choice from Microsoft, you know why 🙂

    So basically you have two choices:

    1) Create a new PerformanceCounter instance each time, using your FindSpawnedProcessPerfCounter method.

    2) Follow the steps described in KB281884 to change the pattern from ProcessName#InstanceNumber to ProcessName_ProcessID.

    The problem of the first solution is that it requires some CPU time to build a new instance each time.

    The problem of the second solution is that the registry modification also impacts all programs that are also using this performance counter. And it requires to modify the registry before launching your app.

    Last option you have, is to not use Performance counters at all. If you are only interested in the ProcessorTime information, there are some Kernel32 functions you could call using P/Invoke to retrieve it.

    EDIT:

    The Process class also provides UserProcessorTime and PrivilegedProcessorTime (kernel processor time) properties. Both return a TimeSpan instance (= amount of time), so to retrieve a percentage of processor time, you’ll have to do some computation by yourself (involving the refresh period and the processor times).

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

Sidebar

Related Questions

I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
I have a hierarchy that looks something like this: class Base { public: void
I'm using boost.python to make python-modules written in c++. I have some base class
Below is the code that I am using: This is my app/models file: class
I am using this class to load multiple images synchronously. Somehow the loader doesn't
I tried to send an email using this class below, but no success, no
i have one single class for margin and i am using this class to
I'm using this php class ( http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php ) for creating the different sized images
I am using this WebCrawler class http://phpcrawl.cuab.de . There is a method named setTmpFile()
I've embedded a SWF into a class using this syntax above my class definition:

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.