Monitor network performance for a specific process
Asked Answered
C

1

8

I'm writing a program that uses the .NET performance counter to get CPU, Memory and Network usage for a specific process.

For example, to get the CPU and Memory data for Explorer, I create performance counters like this:

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "% Processor Time";
PC.InstanceName = "Explorer";

PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = "Explorer";

Unfortunately, there is no property for the Process categoryName that allows me to get network usage for that process. I can use the Network Interface categoryName to get overall network usage on any particular NIC, but cannot isolate it to any given Process.

Cortes answered 24/4, 2013 at 18:57 Comment(2)
it looks like there is something similar asked before:#438740Rummy
This is an architectural limitation. These counters are implemented by the network driver stack. Which runs in kernel mode, it doesn't have usermode process awareness. Same kind of reason why FileSystemWatcher cannot tell you which process modified a file.Eleanore
S
0

I suppose you can only get the network usage for the entire system. You can use this code:

GetCounterValue(_netRecvCounters[index], "Network Interface", "Bytes Received/sec",      _instanceNames[index]):

double GetCounterValue(PerformanceCounter pc, string categoryName, string counterName, string instanceName)
{
    pc.CategoryName = categoryName;
    pc.CounterName = counterName;
    pc.InstanceName = instanceName;
    return pc.NextValue();  
}

With PerformanceCounters, you can only get the values that Microsoft want's you to have access to.

Shortfall answered 25/4, 2013 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.