Title: OS Processes monitoring
Slug: os-processes-monitoring
Date: 2006-04-18 11:50:00
Author: Kartones
Lang: en
Tags: Development, .NET
Description: A guide on monitoring OS processes using .NET.

 <p>For a personal app I've needed to pool the OS for running processes, and with
.NET couldn't be easier...</p>
<p>Add a reference and to using statements...</p><pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">using</span> System.ServiceProcess;<br><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">using</span> System.Diagnostics;</span></pre>
<p>And with just one line of code it's done</p><pre><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">Process[] runningProcesses <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> Process.GetProcesses(System.Environment.MachineName);</span></pre>
<p>The <b>Process</b> class gives some info, like process ID, startup
time, memory in use... Beware that some process may throw some
<b>Win32Exceptions</b> when trying to access data (it happened to me
with the <i>Idle</i> process when trying to get startup time).</p>
<p>Oh, and with that class you can launch (and monitor) new process too, even
attaching events to them (like a wonderful <i>OnExited()</i> event
handler).</p>
