Reputation: 14285
i have got a 2 exe (console)
first exe provides facility to convert video formats. second exe provides facility to split video.
in my application i have got 2 buttons with which both process are working fine separately. but now i wants to make it work on single click. means first it should convert video using first exe and then split that using second exe.
the problem is that how to find that first exe has finished its work so that i can start second exe to work on output's of first exe.
i am running both exe by creating process.
NOTE: my both exe gets close when they done their work, so may be we can check for existing of there process but i wants experts opinion for this.
Thanks
Upvotes: 1
Views: 2397
Reputation: 5153
If you are using a GUI, it will halt if you use WaitForExit.
Here's an asynchronous example. You will have to adapt it to your needs:
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
class ConverterClass
{
private Process myProcess = new Process();
private bool finishedFlag = false;
/* converts a video asynchronously */
public void ConvertVideo(string fileName)
{
try
{
/* start the process */
myProcess.StartInfo.FileName = "convert.exe"; /* change this */
/* if the convert.exe app accepts one argument containing
the video file, the line below does this */
myProcess.StartInfo.Arguments = fileName;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
/* handle exceptions here */
}
}
public bool finished()
{
return finishedFlag;
}
/* handle exited event (process closed) */
private void myProcess_Exited(object sender, System.EventArgs e)
{
finishedFlag = true;
}
public static void Main(string[] args)
{
ConverterClass converter = new ConverterClass();
converter.ConvertVideo("my_video.avi");
/* you should watch for when the finished method
returns true, and then act accordingly */
/* as we are in a console, the host application (we)
may finish before the guest application (convert.exe),
so we need to wait here */
while(!converter.finished()) {
/* wait */
Thread.Sleep(100);
}
/* video finished converting */
doActionsAfterConversion();
}
}
When the program exits, finishedFlag will be set to true, and the finished() method will start returning that. See Main for "how you should do it".
Upvotes: 3
Reputation: 30840
How about something like :
Process p1 = Process.Start("1.exe");
p1.WaitForExit();
Process p2 = Process.Start("2.exe");
Upvotes: 3
Reputation: 405
if it is in windows just call WaitForSingleObject on the handle returned by CreateProcess
Upvotes: 1