Or change it up a little so you can subscribe to the Process.Exited() Event:
Main Topics
Browse All TopicsI'm running the following code to execute FFMPEG from my webpage button click:
Process p22;
ProcessStartInfo info22 = new ProcessStartInfo();
info22.FileName = "c:\\3gffmpeg.exe";
info22.WindowStyle = ProcessWindowStyle.Hidden;
info22.Arguments = " -i c:\\" + realfilename + " -f 3gp -sameq -ar 22050 c:\\test.3gp";
p22 = Process.Start(info22);
I'd like to run set some code to run when that process is completed, if possible. some sort of try/catch relationship, where the catch{code} would be the code to run after process completes.
is there anything like this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
getting a compilation error with idle mind's answer:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0165: Use of unassigned local variable 'p22'
in the page load i have exactly what i pasted in original question but i added this:
p22.Exited += new EventHandler(p2_Exited);
i added that right above p22 = Process.Start(info22);
then outside the page_load i have this:
void p22_Exited(object sender, EventArgs e)
{
MailMessage message22 = new MailMessage();
message22.From = new MailAddress("");
message22.Bcc.Add(new MailAddress(""));
message22.Subject = "file finished";
message22.Body = "file finished";
SmtpClient client22 = new SmtpClient();
client22.Send(message22);
}
it highlights this line: Line 285: p22.Exited += new EventHandler(p22_Exited);
Um, p22 instance will be out of scope after the button click is executed and the page request is serviced. That was the reason that I did not propose this solution...
Also you should check if your process exit code is 0 (It has finished processing OK)
Process p22;
ProcessStartInfo info22 = new ProcessStartInfo();
info22.FileName = "c:\\3gffmpeg.exe";
info22.WindowStyle = ProcessWindowStyle.Hidden;
info22.Arguments = " -i c:\\" + realfilename + " -f 3gp -sameq -ar 22050 c:\\test.3gp";
p22 = Process.Start(info22);
p22.WaitForExit();
MailMessage message22 = new MailMessage();
message22.From = new MailAddress("");
message22.Bcc.Add(new MailAddress(""));
if (p22.ExitCode == 0)
{
message22.Subject = "file finished OK";
message22.Body = "file finished OK";
}
else
{
message22.Subject = "file finished with errors";
message22.Body = "file finishedwith errors"+ p22.ExitCode ;
}
SmtpClient client22 = new SmtpClient();
client22.Send(message22);
Sorry...didn't see that this was ASP.net. My solution is geared for WinForms.
Regarding the error, though, I changed the way the Process() class was being used.
FIRST I instantiated p22:
Process p22 = new Process();
Then I DIRECTLY populated the StartInfo() structure instead of creating a separate one:
p22.StartInfo.FileName = "c:\\3gffmpeg.exe";
p22.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p22.StartInfo.Arguments = " -i c:\\" + realfilename + " -f 3gp -sameq -ar 22050 c:\\test.3gp";
Then I enabled raising events and subscribed to the Exited()...BEFORE calling Start():
p22.EnableRaisingEvents = true;
p22.Exited += new EventHandler(p22_Exited);
Finally, we call the Start() method on the INSTANCE we have already created:
p22.Start();
Again, though, I was thinking of WinForms so I don't know how this applies to Web stuff...
mas, does "waitforexit" mean the user experience will be a hanging page until the ffmpeg conversion is complete, and before any of the rest of the code can finish? cus if so that seems like a problem. i'd like the process and after-code to be 'queued' up or whatever, to fire in background so user can move on with his life.
idle, so i can't do what you're proposing if i'm trying to throw all this into an ASPX's page_load?
Yes, but only for that user request.
I do not think the page_load is a good solution. You can create a new thread and put all your code there.
the thread will be alive for the lifetime of your application pool.
public void DoWork(object data)
{
Process p22;
ProcessStartInfo info22 = new ProcessStartInfo();
info22.FileName = "c:\\3gffmpeg.exe";
info22.WindowStyle = ProcessWindowStyle.Hidden;
info22.Arguments = " -i c:\\" + realfilename + " -f 3gp -sameq -ar 22050 c:\\test.3gp";
p22 = Process.Start(info22);
p22.WaitForExit();
MailMessage message22 = new MailMessage();
message22.From = new MailAddress("");
message22.Bcc.Add(new MailAddress(""));
if (p22.ExitCode == 0)
{
message22.Subject = "file finished OK";
message22.Body = "file finished OK";
}
else
{
message22.Subject = "file finished with errors";
message22.Body = "file finishedwith errors"+ p22.ExitCode ;
}
SmtpClient client22 = new SmtpClient();
client22.Send(message22);
}
If you need to pass data see Start(data) method of thread on how to use it. I use a similar construct succesfully for doing background processing of long running tasks.
Business Accounts
Answer for Membership
by: mas_oz2003Posted on 2009-10-26 at 19:43:31ID: 25668778
use the WaitForExit method.
Process p22;
ProcessStartInfo info22 = new ProcessStartInfo();
info22.FileName = "c:\\3gffmpeg.exe";
info22.WindowStyle = ProcessWindowStyle.Hidden;
info22.Arguments = " -i c:\\" + realfilename + " -f 3gp -sameq -ar 22050 c:\\test.3gp";
p22 = Process.Start(info22);
p22.WaitForExit();
//do something after completion