2014-12-26 4 views
1

exe를 통해 BCP 명령을 실행 중이며 50000 개의 행을 복사 한 후 멈 춥니 다. 몇 가지 포럼을 살펴본 결과, 최대 출력 제한보다 코드에서 StandardOuputReader를 사용하면 50000 행에 가까운 것이 나에게 있다는 것을 알게되었습니다. 50000 개가 넘는 행을 리디렉션 출력에서 ​​실행할 수있는 방법이 있습니까 나가. 표준 출력 리더기 BCP 도구를 멈춤

이 코드

내가
proc.StartInfo.RedirectStandardOutput = false; 

을 가지고 있지만 내가 출력을 볼 수, 그것은 참으로 갖고 싶어 여기에 작동합니다.

private static void RunBatch(string Fullfilepath, string BatchFilePathDumpFlatFile) 
     { 
      mLogger.Error("RunBatch Start=== >"); 
      mLogger.Error("Batch Filepath " + Fullfilepath + '\n' + "Batch File Directory " + BatchFilePathDumpFlatFile); 
      Process proc = null; 
      string targetDir = BatchFilePathDumpFlatFile; 
      proc = new Process(); 
      proc.StartInfo.WorkingDirectory = targetDir; 
      proc.StartInfo.FileName = Fullfilepath; 

      //proc.StartInfo.CreateNoWindow = true; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.Arguments = "/c"; 
      proc.StartInfo.RedirectStandardError = true; 
      proc.StartInfo.RedirectStandardOutput = false; 
      proc.Start(); 
      proc.WaitForExit(); 
      string output = proc.StandardOutput.ReadToEnd(); 
      proc.WaitForExit(); 
      string error = proc.StandardError.ReadToEnd(); 
      proc.WaitForExit(); 
      mLogger.Error("Output from batch " + output); 
      mLogger.Error("Error From Batch " + error); 



     } 

업데이트 1이 실수가 내가 사용하고 있습니다

private static void RunBatch(string Fullfilepath, string BatchFilePathDumpFlatFile) 
     { 
      mLogger.Error("RunBatch Start=== >"); 
      mLogger.Error("Batch Filepath " + Fullfilepath + '\n' + "Batch File Directory " + BatchFilePathDumpFlatFile); 
      Process proc = null; 
      string targetDir = BatchFilePathDumpFlatFile; 
      proc = new Process(); 
      proc.StartInfo.WorkingDirectory = targetDir; 
      proc.StartInfo.FileName = Fullfilepath; 

      //proc.StartInfo.CreateNoWindow = true; 
      proc.StartInfo.UseShellExecute = false; 
      proc.StartInfo.Arguments = "/c"; 
      proc.StartInfo.RedirectStandardError = true; 
      proc.StartInfo.RedirectStandardOutput = true; 
      proc.Start(); 
      string output = proc.StandardOutput.ReadToEnd(); 
      proc.WaitForExit(); 
      string error = proc.StandardError.ReadToEnd(); 
      proc.WaitForExit(); 
      mLogger.Error("Output from batch " + output); 
      mLogger.Error("Error From Batch " + error); 



     } 

때문에 여전히 BCP이 중지되고 난 코드의 EXE를 중지 할 때 실행을 시작합니다.

답변

2

이것은 고전적인 교착 상태입니다. StandardOutput을 완전히 읽기 전에 WaitForExit으로 전화하지 마십시오.

출력을 리디렉션하면 해당 스트림의 모든 StandardOutput 스트림이 읽히기 전에 프로세스가 종료되지 않습니다. 따라서 WaitForExit을 호출하면 시작된 프로세스가 종료 될 때까지 대기하고, 자식 프로세스는 부모 프로세스가 완료되고 교착 상태가되기 전에 출력 스트림을 완전히 읽을 때까지 대기합니다.

Msdn은 교착 상태를 피하기위한 설명과 코드 샘플을 제공합니다.

동기 읽기 작업은 호출자의 StandardOutput 스트림 읽기와 해당 스트림에 기록하는 하위 프로세스 간의 종속성을 도입합니다. 이러한 종속성으로 인해 교착 상태가 발생할 수 있습니다. 호출자가 하위 프로세스의 리디렉션 된 스트림에서 읽으면 자식에 종속됩니다. 호출자는 자식이 스트림에 쓰거나 스트림을 닫을 때까지 읽기 작업을 기다립니다. 하위 프로세스가 리디렉션 된 스트림을 채우기에 충분한 데이터를 쓰면 부모 프로세스에 종속됩니다. 자식 프로세스는 부모가 전체 스트림을 읽거나 스트림을 닫을 때까지 다음 쓰기 작업을 기다립니다. 교착 s 태는 호출자와 하위 프로세스가 조작을 완료하기 위해 서로 대기하고 둘 다 진행할 수 없게됩니다. 호출자와 하위 프로세스 간의 종속성을 평가하여 교착 상태를 피할 수 있습니다.

// Start the child process. 
Process p = new Process(); 
// Redirect the output stream of the child process. 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "Write500Lines.exe"; 
p.Start(); 
// Do not wait for the child process to exit before 
// reading to the end of its redirected stream. 
// p.WaitForExit(); 
// Read the output stream first and then wait. 
string output = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

코드 예제 p.WaitForExit 전에 p.StandardOutput.ReadToEnd를 호출하여 교착 상태를 피할 수 있습니다. 부모 프로세스가 p.StandardOutput.ReadToEnd 전에 p.WaitForExit를 호출하고 자식 프로세스가 리디렉션 된 스트림을 채우기에 충분한 텍스트를 쓰는 경우 교착 상태가 발생할 수 있습니다. 부모 프로세스는 자식 프로세스가 종료 될 때까지 무기한 대기합니다. 하위 프로세스는 부모가 전체 StandardOutput 스트림에서 읽을 수 있도록 무기한 대기합니다.

표준 출력 스트림과 표준 오류 스트림 모두에서 모든 텍스트를 읽을 때도 비슷한 문제가 발생합니다. 예를 들어 다음 C# 코드는 두 스트림 모두에서 읽기 작업을 수행합니다.

위의 거의 모든 내용이 msdn에서 가져온 것이므로 교착 상태가 발생하지 않도록하시기 바랍니다.

+0

나는 그것을 시도했지만 BCP를 계속 복사하고 50000 개의 행을 얻지는 않지만 평범한 파일을 만든 다음 정지합니다. – Ke7in

+0

@ Ke7in 어디에서 멈 춥니 다? 어느 선 이요? 업데이트 된 코드를 게시하십시오. –

+0

헤이 작동, 실제로 그것이 작동하지 않는 것 같아 시간이 걸립니다. – Ke7in