Click to See Complete Forum and Search --> : How can I get a callback from Process.Start?
purpleflash
August 10th, 2008, 08:23 AM
I'm using this code:
Process myProcess = new Process();
myProcess.StartInfo.FileName = filename;
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();
to execute a process which emails a file... works fine, except that after it's sent I need to delete the file.
I'm guessing I need to set a callback method which will get called when the process ends?
How do I do this, or if that's not the right way to accomplish this let me know, thanks.
Pale
August 10th, 2008, 10:27 AM
You can use the Process Control located in your toolbox.
And then.
Set:
myprocess.EnableRaisingEvents = true;
and Create the following event handler:
private void myprocess_Exited(object sender, EventArgs e)
{
try
{
System.IO.File.Delete("EmailFile");
}
catch
{
}
}
Change the ("EmailFile") to the location of the file you want to delete.
purpleflash
August 10th, 2008, 10:54 AM
Process myProcess = new Process();
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.StartInfo.FileName = filename;
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.RedirectStandardOutput = false;
myProcess.Start();
...
private void myProcess_Exited(object sender, EventArgs e)
{
try
{
System.IO.File.Delete("c:\\program files\\msts\\appointment.vcs");
}
I'm wondering if, since myProcess fires off the user's email client, it has no way to tell when that program (the email client) is closed by the user, hence it doesn't raise the Exited event? Seems like it should though..
Pale
August 10th, 2008, 11:13 AM
You have to use the Visual Studio process control from the toolbox.
it looks like this:
http://i287.photobucket.com/albums/ll159/Pale091/ProcessControl.jpg
Then you must go into the properties of that and set the location of the executable that you want to monitor. In your case the email client.
Once you have got it monitored, it will wait for the process to exit and then, go through you exited event handler.
*Edit* I just noticed that your trying to delete a file in the program files folder.
If the user is running vista your app will need administrative privileges to be permitted to delete that file. I would suggest putting the file somewhere else, such as "Mydocs\Yourappname\Thefile.file".
purpleflash
August 10th, 2008, 03:25 PM
Thanks for the help.
This is a WPF app so there's no Process item in my toolbox... guess I'll have to find another way to do this.
Arjay
August 10th, 2008, 03:43 PM
You have to use the Visual Studio process control from the toolbox.No you don't. While you can use the process item from the toolbox (if available), you can also create a Process object directly in code.
purpleflash
August 10th, 2008, 04:30 PM
Why won't what I have already put together work... FWIW I have no idea what the executable will be the the process runs... it's the user's email client.
So that could be Outlook, Thunderbird, etc.; whatever the Shell has identified as the email client.
Arjay
August 10th, 2008, 08:29 PM
Why won't what I have already put together work?What doesn't work? Can you be specific? How far does the application get when you step through it in a debugger? Are you using a debugger? Please answer.
purpleflash
August 11th, 2008, 08:07 PM
And I'm using the VS2008 debugger.
Problem is the process Exited code never gets called.
Arjay
August 11th, 2008, 08:39 PM
Does the process exit? Perhaps the email client remains hidden? Look in task manager to be sure.
Try the code loading up notepad.exe and then close notepad to see if it gets called.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.