473,412 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

System.Diagnostics.Process.Exited event

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
2 8157
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dinoo | last post by:
I am looking this for a long time now I have a Windows Form in C#? I opened another window (of other application) or MS Word Document If user closes the document and other window is there some...
0
by: [Joe] | last post by:
I create a process which run my application with parameters, but when I run the application I can't see the output untill the proccess finisting to run the application. How can I see the out put...
2
by: Sebastian Sosna | last post by:
Hello NG, iam having a problem with the code block below. All what i do is to trace processes that exited. Iam using a 5 sec timer to call the following Method: (wich i think is causing the...
2
by: Andy | last post by:
hi all, I've created a windows server that will watch to make sure another process stays running. It looks for the Process and attaches to the Exited event, which works fine. If the program...
1
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
2
by: T | last post by:
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...
2
by: Ron St-Pierre | last post by:
We're developing a java app and are using postgres as the database. On our dev server I started the app, closed it, but the java process was still open so I killed it, which caused the above error....
3
by: steven deng | last post by:
I want to know when a clickonce app exits. I have code snippet below. But the Exited event is not triggered. If I change the process to Notepad.exe(see comment line), it works fine. Any suggestion...
2
by: Twistfactory | last post by:
Hi all, Is there some way to execute an external process that will execute asynchronously and then notify me when it's completed? Any sort of a callback or class member that I could check...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.