|
-
August 10th, 2008, 08:23 AM
#1
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.
-
August 10th, 2008, 10:27 AM
#2
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.
Last edited by Pale; August 10th, 2008 at 10:33 AM.
-
August 10th, 2008, 10:54 AM
#3
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..
-
August 10th, 2008, 11:13 AM
#4
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:

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".
Last edited by Pale; August 10th, 2008 at 11:18 AM.
-
August 10th, 2008, 03:25 PM
#5
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.
-
August 10th, 2008, 03:43 PM
#6
Re: How can I get a callback from Process.Start?
 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.
-
August 10th, 2008, 04:30 PM
#7
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.
-
August 10th, 2008, 08:29 PM
#8
Re: I figured I could do this in code..
 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.
-
August 11th, 2008, 08:07 PM
#9
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.
-
August 11th, 2008, 08:39 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|