By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

System.Diagnostics.Process.Exited event

P: n/a
T
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");
}
}
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


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

"T" <T@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
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");
}
}

Jul 21 '05 #2

P: n/a
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

In article <B1**********************************@microsoft.co m>,
T@discussions.microsoft.com says...
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");
}
}

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.