2010-12-02 23 views
1

我有一个2 exe(控制台)如何知道由我的应用程序的进程运行的exe已完成其工作

第一个exe提供了转换视频格式的工具。 second exe提供了分割视频的功能。

在我的应用程序中,我有2个按钮,这两个进程工作正常单独罚款。 但现在我想让它在单击时工作。意味着首先它应该使用第一个exe转换视频,然后使用第二个exe分割。

问题是如何找到第一个exe文件已完成其工作,以便我可以启动第二个exe文件来处理第一个exe文件的输出。

我通过创建进程运行这两个exe。

注意:当他们完成他们的工作时,我的两个exe都会关闭,因此可能我们可以检查那里是否存在过程,但是我希望专家对此有所认识。

感谢

回答

3

如果您使用的是图形用户界面,它会停止,如果你使用WaitForExit。
这是一个异步的例子。你将不得不以使其适应您的需求:

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(); 
    } 
} 

当程序退出时,finishedFlag将被设置为true,并完成()方法将开始返回这一点。看到主要的“你应该怎么做”。

+0

未找到函数退出“myProcess.Exited + = new EventHandler(myProcess_Exited);” – 2010-12-02 08:49:58

1

如果是在Windows只是调用WaitForSingleObject由CreateProcess的

3

返回的句柄如何像:

Process p1 = Process.Start("1.exe"); 
p1.WaitForExit(); 
Process p2 = Process.Start("2.exe"); 
相关问题