2010-12-02 81 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"); 
相關問題