3

From my C# application, I'm trying to get notification from another (specific) application when it is closing (just when its process ends). From what I found, hook on this process is a possible solution, but I didn't managed to make it work. Can anyone help me on that or propose another solution.

svick
  • 236,525
  • 50
  • 385
  • 514
SWE
  • 485
  • 1
  • 4
  • 19

4 Answers4

3

Use WMI to monitor for a process closing event. There's some example code here.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
3

You can use the Process class to do this, specifically, its Exited event:

var process = Process.GetProcessesByName("notepad").Single();
// or Process.GetProcessById() if you have the ID
process.EnableRaisingEvents = true;
process.Exited += YourHandler;
svick
  • 236,525
  • 50
  • 385
  • 514
1

I've done these kind of things simply by checking a process every 100 ticks. it may be expensive, but that depend on your need. u can use Process.GetProcessById() for that

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
1

There is a really simple way of doing this, provided if the process you're after is a user-process, or you're running as administrator:

var processes = Process.GetProcessesByName("Notepad");
Console.WriteLine("waiting...");
foreach(var p in processes )
    p.WaitForExit();
Console.WriteLine("notepad has exited...");

The easiest way to use this is to spawn a new Task that will run this code and then leverage the callback to do your aditional work after the process has exited.

Seph
  • 8,472
  • 10
  • 63
  • 94