2010-12-02 2 views
1

나는 2 exe (콘솔)을 가지고 있습니다내 응용 프로그램의 프로세스에서 실행되는 exe를 확인하는 방법은 작업을 완료했습니다

첫 번째 exe는 비디오 형식을 변환하는 기능을 제공합니다. 두 번째 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로 설정되고, 완성 된() 메소드는 반환이 시작됩니다 : 당신은 당신의 요구에 적응해야합니다. "어떻게해야 하는가?"를 위해 Main을 보라.

+0

"myProcess.Exited + = new EventHandler (myProcess_Exited);에서 종료됩니다." –

+0

모든 것이 여기에 좋습니다. test.cs (27,26) : 경고 CS0168 : 변수 'ex'가 선언되었지만 사용되지 않음 컴파일 성공 - 1 개 경고 –

1

이 창에있는 경우 바로 CreateProcess를

3

에 의해 반환 된 핸들의 WaitForSingleObject를 호출하는 방법과 같은 약 : 당신이 GUI를 사용하는 경우

Process p1 = Process.Start("1.exe"); 
p1.WaitForExit(); 
Process p2 = Process.Start("2.exe"); 
관련 문제