Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: Instance 'Name of instance' does not exist in the specified Category

When I create and use performance counters like this:

private readonly PerformanceCounter _cpuPerformanceCounter;
public ProcessViewModel(Process process)
        {

             _cpuPerformanceCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
        }

public void Update()
        {
            CPU = (int)_cpuPerformanceCounter.NextValue() / Environment.ProcessorCount; // Exception
        }

... I get an exception Instance 'Name of instance' does not exist in the specified Category and don't understand why.

P.S. Code

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <settings>
      <performanceCounters enabled="true"/>
    </settings>
  </system.net>
</configuration>

... included in App.config.

like image 476
Aleksandr Vishnyakov Avatar asked Apr 29 '11 13:04

Aleksandr Vishnyakov


2 Answers

Adding on to previous posts, I have seen processes being formatted like <ProcessName>_<ProcessId> - depending on the OS you are running your app on (Win XP, Win Vista, Win 7, Win 2003 or 2008 Server). In order to have a reliable way to identify your process name for obtaining other performance counters down the road, a function could look like this:

    private string ObtainProcessName()
    {
        string baseProcessName;
        string processName = null;
        int processId;
        bool notFound = true;
        int processOptionsChecked = 0;
        int maxNrOfParallelProcesses = 3 + 1;

        try
        {
            baseProcessName = Process.GetCurrentProcess().ProcessName;
        }
        catch (Exception exception)
        {
            return null;
        }

        try
        {
            processId = Process.GetCurrentProcess().Id;
        }
        catch (Exception exception)
        {
            return null;
        }

        while (notFound)
        {
            processName = baseProcessName;
            if (processOptionsChecked > maxNrOfParallelProcesses)
            {
                break;
            }

            if (1 == processOptionsChecked)
            {
                processName = string.Format("{0}_{1}", baseProcessName, processId);
            }
            else if (processOptionsChecked > 1)
            {
                processName = string.Format("{0}#{1}", baseProcessName, processOptionsChecked - 1);
            }

            try
            {
                PerformanceCounter counter = new PerformanceCounter("Process", "ID Process", processName);
                if (processId == (int)counter.NextValue())
                {
                    notFound = !true;
                }
            }
            catch (Exception)
            {
            }
            processOptionsChecked++;
        }
        return processName;
    }
like image 85
Wolfgang Avatar answered Oct 27 '22 04:10

Wolfgang


I think your issue happens when there are more than one process with the same name. What PerfMon does then is append #1, #2, etc to the process name. So that means MyApp.exe executed twice will cause this exception when you try to read the performance monitor for "MyApp". Here's a link to one way of solving this: Read performance counters by pid

like image 43
Haukman Avatar answered Oct 27 '22 04:10

Haukman