Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Created December 13, 2019 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsscoder/255befc5221890e679e89e781befb190 to your computer and use it in GitHub Desktop.
Save gsscoder/255befc5221890e679e89e781befb190 to your computer and use it in GitHub Desktop.
Demonstrates how to detect end of a running process
// purpose: detect process end
// demo:
// - launch firefox
// - launch this program
// - close firefox and see 'died'
// notes:
// - may need sudo on *nix systems
using System;
using System.Diagnostics;
using System.Linq;
class Program
{
static Process _process;
static void Main(string[] args)
{
var allProcesses = Process.GetProcesses();
_process = allProcesses.Where(x => x.ProcessName.Contains("firefox")).First();
_process.EnableRaisingEvents = true;
_process.Exited += new EventHandler(OnExit);
Console.WriteLine("Hit a key to exit");
Console.ReadLine();
}
static void OnExit(object sender, System.EventArgs e)
{
Console.WriteLine("died");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment