views:

610

answers:

6

Hello world.

I would like to find a way to loop through all the active processes and do diagnostics checks on them (mem usage, cpu time etc) kinda similar to the task manager.

The problem is broken down into two parts:

  1. Finding all the processes
  2. Finding diagnostics attributes about them

I am not sure even in what namespace to go looking about it. Any help / tips / links is grateful.

+12  A: 

Finding all of the processes

You can do this through the Process class

using System.Diagnostics;
...
var allProcceses = Process.GetProcesses();

Running Diagnostics

Can you give us some more information here? It's not clear what you want to do.

The process class provides a bit of information though that might help you out. It is possible to query this class for

  • All threads
  • Main Window Handle
  • All loaded modules
  • Various diagnostic information about Memory (Paged, Virtual, Working Set, etc ...)
  • Basic Process Information (id, name, disk location)

EDIT

Op mentioned they want to get memory and CPU information. These properties are readily available on the Process class (returned by GetProcesses()). Below is the MSDN page that lists all of the supported properties. There are various memory and CPU ones available that will suite your needs.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

JaredPar
A: 

What operating system are you using? I take it from your C# tag that it's windows?

If so, check out WMI, particularly the Win32_Process class. Here's the MSDN reference: http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx

As well as a couple of usage scenarios here (such as getting a list of processes): http://msdn.microsoft.com/en-us/library/aa394599(VS.85).aspx

CapBBeard
Yes it is windows (It says in the topic).
Statement
Haha sorry, massive overlook on my part!
CapBBeard
+3  A: 

Finding all processes is rather easy actualy:

using System.Diagnostics;

            Process[] processes = Process.GetProcesses();

            foreach (Process process in processes)
            {
               //Get whatever attribute for process
            }
Mez
A: 

Well, I've has good results with Startup Commander and Fix-It Utilities 9 - they both show you what processes are running, and which one you can switch off upon startup. Be careful, as sometimes you may switch one off that can only be loaded back whilst in safe mode.

Note: FU9's StartupCommander explains the running processes.

More of a response for beginners.

Adrian33
Well, good for beginners perhaps, but I'm looking for ways to code it.
Statement
+1  A: 

JaredPar already pointed out the Process class, so I'll just add, that you should be aware, that the class takes a snapshot of the process' information when the instance is created. It is not a live view. To update it you have to call Refresh() on the instance.

Also keep in mind, that the process may close while you are inspecting it, so be prepared to catch exceptions and handle them accordingly.

And finally if you call Process.GetProcesses() you will also get the pseudo processes "idle" and "system". IIRC they have specific process IDs so you can easily filter them out.

Brian Rasmussen
Thanks for the gotcha. I will be sure to keep this in mind +1
Statement
A: 

Well you can do this in powershell

1.Finding all the processes

get-Process

2.Finding diagnostics attributes about them

get-Process | where-object { $_.Handles -gt 200 }

The above code will return all processes with over 200 handles, you can specify your diagnostic attributes in this manner easily.

For more information on how to handle processes using powershell see this

Binoj Antony