Process class provides GetProcess method to get all running processes. The GetProcesses
method returns collection of process. Similarly GetCurrentProcess method returns currently
running process. Process class is available in System.Diagnostics namespace.
Let’s have a look on below code.
Let’s have a look on below code.
void
ProcessThreads_Loaded(object sender, RoutedEventArgs e)
{
foreach (Process p in Process.GetProcesses())
Console.WriteLine("{0} | {1} | {2}", p.ProcessName, p.Id,
p.Threads.Count);
Console.WriteLine("Current Process");
Process
current = Process.GetCurrentProcess();
Console.WriteLine("{0} | {1} | {2}", current.ProcessName,
current.Id, current.Threads.Count);
current.Id, current.Threads.Count);
}
Output
svchost | 616 | 13
SearchIndexer | 3344
| 14
lsm | 584 | 12
spoolsv | 1764 | 13
COH32 | 5112 | 3
WPFTestApplication.vshost
| 2660 | 14
lsass | 576 | 9
RegSrvc | 2348 | 4
COH32 | 4908 | 1
SNAC | 1360 | 13
services | 568 | 6
explorer | 5884 | 26
svchost | 956 | 19
iexplore | 756 | 16
ccSvcHst | 1540 | 36
svchost | 3704 | 5
rundll32 | 3868 | 2
…
…
…
Current Process
WPFTestApplication.vshost
| 2660 | 14
The above output displays all the processes with its name,
id and total thread count. The last line displays currently running process.
Displaying Process Threads
We can get list of all the threads running under process using Threads collection of Process instance. Let’s have a look on below code.
void
ProcessThreads_Loaded(object sender, RoutedEventArgs e)
{
foreach (Process p in Process.GetProcesses())
{
Console.WriteLine("{0} | {1} | {2}", p.ProcessName, p.Id,
p.Threads.Count);
DisplayThreads(p);
}
}
private void DisplayThreads(Process
p)
{
foreach (ProcessThread thread in
p.Threads)
{
Console.WriteLine(" {0}
| {1} | {2} | {3}", thread.Id,
thread.PriorityLevel,
thread.ThreadState, thread.TotalProcessorTime);
}
}
Output
wininit | 468 | 3
472 | Highest | Wait |
00:00:00.4992032
556 | Normal | Wait | 00:00:00
564 | Normal | Wait | 00:00:00
LMS | 1252 | 8
1304 | Normal | Wait |
00:00:00.0468003
1336 | Normal | Wait | 00:00:00
1924 | Normal | Wait |
00:00:00.2496016
2072 | Normal | Wait |
00:00:00.0624004
2180 | Normal | Wait | 00:00:08.6424554
1692 | Normal | Wait | 00:00:00
5552 | Normal | Wait | 00:00:00
3700 | Normal | Wait | 00:00:00
…
…
…
Above code displays list of processes along with their ProcessThreads.
See Also –
How to create custom Application Domain?
How to get system hardware information?
How to pass data as an argument to Thread?
No comments:
Post a Comment