System.Diagnostics.Process.Exited event

G

Guest

How do you catch the "Exited" event when a process is terminated. In the following code sample the "myProcess_Exited" event is never called. Any help would be appreciated.


using System;
using System.Diagnostics;
using System.Threading;

class ConsoleProcessApp
{
public static void Main()
{
try
{
Process myProcess;
myProcess = Process.Start("Notepad.exe");
myProcess.Exited +=new EventHandler(myProcess_Exited);

// Display physical memory usage.
// Discard cached information about the process.
myProcess.Refresh();
// Print working set to console.
Console.WriteLine("Physical Memory Usage: " + myProcess.WorkingSet.ToString());
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow();
// Free resources associated with process.
myProcess.Close();
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: ");
Console.WriteLine(e.Message);
}
}

private static void myProcess_Exited(object sender, EventArgs e)
{
Console.WriteLine("Event myProcess_Exited has been called");
}
}
 
D

David Levine

Your app has exited before the process you launched has a chance to run and
exit itself.

T said:
How do you catch the "Exited" event when a process is terminated. In the
following code sample the "myProcess_Exited" event is never called. Any
help would be appreciated.
 
S

Sunny

Hi,

Set the 'EnableRaisingEvents' property of the process object to true.

And you can add
myProcess.WaitForExit();

just after myProcess.CloseMainWindow();


Hope that helps
Sunny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top