For the current program, the Process.exited event does not work, the following code:
static void Main ()
{
var pro = Process.getcurrentprocess ();
Pro. EnableRaisingEvents = true;
Pro. Exited + + new EventHandler (pro_exited);
}
static void Pro_exited (object sender, EventArgs e)
{
throw new notimplementedexception ();
}
The result will not be thrown by any exceptions.
The solution is to use Appdomain.processexit, which is invoked when the default application domain is unloaded due to a process exit:
static void Main ()
{
AppDomain.CurrentDomain.ProcessExit + = new EventHandler (currentdomain_processexit);
}
static void Currentdomain_processexit (object sender, EventArgs e)
{
throw new notimplementedexception ();
}
Run the code, the exception will be thrown smoothly.
However, if the process is forced to quit, none of the above events will be run.
If a program is used to monitor the process.exited event of another program, the process.exited will still run even if another process is forced to end.
Code:
static void Main () {//Open notepad var pro = new Process (); Pro.
Startinfo.filename = "Notepad"; Pro.
EnableRaisingEvents = true; Pro.
exited + = new EventHandler (pro_exited); Pro.
Start (); Pro.
WaitForExit (); static void Pro_exited (object sender, EventArgs e) {Console.WriteLine ("Another process has been terminated");