I have two methods. One method contains ThreadPool and ThreadPool runs second method.
Code snippet below:
static void Main()
{
var processes = GetProcessCounter();
foreach (var process in processes)
{
var tmp = process;
ThreadPool.QueueUserWorkItem(WriteCounter, tmp);
Console.Write("\n\n");
}
Console.WriteLine("Any key to exit...");
Console.Read();
}
static void WriteCounter(object obj)
{
var process = (ProcessInstance) obj;
Console.WriteLine(
$"Thread ID: {Thread.CurrentThread.ManagedThreadId}, " +
$"Process Name: {process.ProcessName}, " +
$"Process ID {process.ProcessId} ");
Console.Write("\n");
foreach (var cnt in process.Counters)
{
cnt.NextValue();
Console.WriteLine(
$"ThreadId: {Thread.CurrentThread.ManagedThreadId} | " +
$"Group: {cnt.CategoryName} | " +
$"Process: {cnt.InstanceName} | " +
$"Name: {cnt.CounterName} | " +
$"Value: {cnt.GetCalculatedValue(cnt.CounterName)}");
cnt.Close();
}
}
I see that it is working properly. As I'm counting child cycle will run in a single thread with the parent cycle. But in console i see folowins. All mixed:
Thread ID: 15, Process Name: SearchFilterHost, Process ID 1552
ThreadId: 12 | Group: Process | Process: explorer | Name: % User Time | Value: 0
ThreadId: 13 | Group: Process | Process: mqsvc | Name: % User Time | Value: 0
ThreadId: 14 | Group: Process | Process: SearchProtocolHost | Name: % Processor Time | Value: 0
ThreadId: 4 | Group: Process | Process: RtWLan | Name: % Privileged Time | Value: 0,599904
ThreadId: 15 | Group: Process | Process: SearchFilterHost | Name: % Processor Time | Value: 0
ThreadId: 11 | Group: Process | Process: VsHub | Name: % Privileged Time | Value: 0
Thread ID: 16, Process Name: AAM Updates Notifier, Process ID 1280ThreadId: 12 | Group: Process | Process: explorer | Name: % Privileged Time | Value: 0,142615
ThreadId: 15 | Group: Process | Process: SearchFilterHost | Name: % User Time | Value: 0
ThreadId: 14 | Group: Process | Process: SearchProtocolHost | Name: % User Time | Value: 0
ThreadId: 13 | Group: Process | Process: mqsvc | Name: % Privileged Time | Value: 0
ThreadId: 4 | Group: Process | Process: RtWLan | Name: Virtual Bytes Peak | Value: 1,957683E+08
ThreadId: 16 | Group: Process | Process: AAM Updates Notifier | Name: % Processor Time | Value: 0
What should I do for grouped output by ThreadID?