2014-09-10 3 views
0

데이터 테이블 행로드를 반복하고 PDF가 아직없는 경우 생성하려면 다음 코드를 작성했습니다. 작동하지만 wkhtmltoPDF를 한 번에 실행하므로 30 개의 프로세스가 시작되어 서버가 종료됩니다.시스템 프로세스 시작을 대기열에 넣는 방법

한 번에 하나의 프로세스를 시작하고 이전 프로세스가 실행될 때까지 두 번째 프로세스를 시작하는 가장 좋은 방법은 무엇입니까?

DB db = new DB(); 
      DataTable dtPosters = db.GetDataTable("select * from Invoices where PDFGenerated <> 1 or PDFGenerated is Null"); 

      foreach (DataRow DR in dtPosters.Rows) 
      { 
       //Generate Poster, If exsists (Don't!) 
       Directory.CreateDirectory(string.Format("D:\\Invoices\\")); 
       string FilePath = string.Format("D:\\Invoices\\{0}.pdf", DR["InvoiceNumber"].ToString()); 
       if (!File.Exists(FilePath)) 
       { 
        string CMD = string.Format("C:\\Services\\Automafe\\wkhtmltoPDF.exe http://invoicegen/viewinvoice.aspx?InvoiceNumber={0} {1}", DR["InvoiceNumber"].ToString(), FilePath); 
        System.Diagnostics.Process.Start("cmd", "/c " + CMD); 

       } 
       else 
       { 

        //Update the DB 

       } 

      } 
+0

당신은 프로세스가 아니라 여기에, 닫힐 때 볼 이벤트 리스너를 추가 할 수 있습니다 http://stackoverflow.com/a/8455896/575530 – dumbledad

+0

이 하나의 더 나은'process.WaitForExit()'HTTP : //stackoverflow.com/a/3147920/575530 – dumbledad

답변

0

Process.Start는 새로 만든 프로세스의 상태를 모니터링 할 수있는 프로세스 개체를 반환합니다.

간단하고 방법을 차단하는 것은 각 노동자 wait for the process to exit

var myProcess = System.Diagnostics.Process.Start("cmd", "/c " + CMD); 
myProcess.WaitForExit(); 

을 때 프로세스 종료를 모니터링 할 Exited event handler를 사용하는 것이 더 좋은 방법을 가지고하는 것입니다.

var myProcess = System.Diagnostics.Process.Start("cmd", "/c " + CMD); 
myProcess.Exited += myProcessFinishedHandler; 
관련 문제