Something like this could work. It's a bit of a primitive example that uses Tasks to poll Process.Responding
and fire an event when it returns false. I don't think you'll be able to get around polling the process periodically, at least not in an easy/concise way.
Each Task
will run in a background thread so as to not hang the main thread while it watches over each process.
using System.Threading.Tasks;
namespace System.Diagnostics
{
public class ProcessEventArgs : EventArgs
{
public Process Process { get; protected set; }
public ProcessEventArgs( Process process )
{
this.Process = process;
}
}
public delegate void ProcessNotRespondingEvent( object sender, ProcessEventArgs e );
public class ProcessMonitor
{
public event ProcessNotRespondingEvent NotResponding;
protected Process mProcess;
public ProcessMonitor( Process process )
{
this.mProcess = process;
}
public async void Start()
{
Task t = null;
t = Task.Run( () =>
{
while( this.mProcess.Responding )
t.Wait( 1000 ); //1 second
this.OnNotResponding();
} );
await t;
}
protected void OnNotResponding()
{
if( this.NotResponding == null )
return;
ProcessEventArgs e = new ProcessEventArgs( this.mProcess );
this.NotResponding( this, e );
}
}
}
Example usage:
using System.Linq;
internal static class Program
{
private static void Main()
{
var processes = Process.GetProcessesByName( "devenv" );
var monitors = processes.Select( p => {
var monitor = new ProcessMonitor( p );
monitor.NotResponding += ( s, e ) => {
Console.WriteLine( "Process {0}(pid: {1}) has stopped responding.", e.Process.ProcessName, e.Process.Id );
};
return monitor;
} );
foreach( var monitor in monitors )
monitor.Start();
}
}