How can I get a callback from Process.Start?
I'm using this code:
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.
Re: How can I get a callback from Process.Start?
You can use the Process Control located in your toolbox.
And then.
Set:
Code:
myprocess.EnableRaisingEvents = true;
and Create the following event handler:
Code:
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.
Hmmm, my event handler isn't being called
Code:
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..
Re: How can I get a callback from Process.Start?
You have to use the Visual Studio process control from the toolbox.
it looks like this:
http://i287.photobucket.com/albums/l...essControl.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".
Re: How can I get a callback from Process.Start?
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.
Re: How can I get a callback from Process.Start?
Quote:
Originally Posted by Pale
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.
I figured I could do this in code..
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.
Re: I figured I could do this in code..
Quote:
Originally Posted by purpleflash
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.
I have the code written as shown at the top of the post
And I'm using the VS2008 debugger.
Problem is the process Exited code never gets called.
Re: How can I get a callback from Process.Start?
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.