2009-10-15 6 views
2

내 C++/.NET에서 네이티브 C++ 콘솔 응용 프로그램의 출력을 읽어야합니다. 이것에 대한 많은 기사가 있지만, 대부분의 프로세스가 출력물을 읽는 것을 끝낼 때까지 기다린다. 원하지 않는 것은 프로세스가 "cout-ed"한 직후에 읽을 필요가있다. 그렇게하는 동안 GUI를 차단할 수는 있지만, 그건 내가 스스로 할 수있는 일이다. 나는 두 가지 방법을 시도했다. 하나 :콘솔 프로그램에서 출력 읽기

Diagnostics::Process ^process = gcnew Diagnostics::Process; 
process->StartInfo->FileName = pathToExecutable; 
process->StartInfo->RedirectStandardOutput = true; 
process->StartInfo->UseShellExecute = false; 
process->StartInfo->CreateNoWindow = true; 
process->StartInfo->Arguments = "some params"; 
process->EnableRaisingEvents = true; 
process->OutputDataReceived += gcnew Diagnostics::DataReceivedEventHandler(GUI::Form1::consoleHandler); 
process->Start(); 
    process->BeginOutputReadLine(); 

그리고 핸들러 :

System::Void GUI::Form1::consoleHandler(System::Object^ sendingProcess, System::Diagnostics::DataReceivedEventArgs^ outLine){ 
     GUI::Form1::contentForConsole += outLine->Data + "\n"; 
    } 

그러나 디버거는이 프로세스가 완료 된 후에 만이라고 확인했다. ,

Diagnostics::Process ^process = gcnew Diagnostics::Process; 
process->StartInfo->FileName = pathToExecutable; 
process->StartInfo->RedirectStandardOutput = true; 
process->StartInfo->RedirectStandardError = true; 
process->StartInfo->UseShellExecute = false; 
process->StartInfo->CreateNoWindow = true; 
process->StartInfo->Arguments = "some params"; 

    processStatic = process; // static class member 

process->Start(); 

System::Windows::Forms::MethodInvoker^ invoker = gcnew System::Windows::Forms::MethodInvoker(reader); 
invoker->BeginInvoke(nullptr, nullptr); 

그리고 스레드 기능 프로세스가 완료 될 때까지 그것은의 readline 기능을 기다립니다 :

내 두 번째 시도에

나는 스레드보고있는 사용자 정의 만들려고

System::Void GUI::Form1::reader(){ 
    System::String^ str; 
    while ((str = geogenProcess->StandardOutput->ReadLine()) != nullptr) 
    { 
     contentForConsole += str; // timer invoked handler then displays this, but this line is called only once the process is finished 
    } 
} 

프로세스 실행 가능으로 출력한다을 여러 텍스트 줄은 몇 초에서 몇 분 (실제 작업에 따라 다름)에 이릅니다.

+0

나는 이것에 너무 흥미가있다; 우리는 중요한 장소가 아니더라도 우리 가게에서 비슷한 문제를 겪고 있습니다. –

답변

1

결국 답을 찾을 수있었습니다. 콘솔 출력을 읽는 두 가지 방법 모두 완벽하게 작동합니다. 문제는 콘솔 응용 프로그램에있었습니다. 콘솔 출력을 수동으로 플러시하여 다른 앱에서 사용할 수있게해야한다는 것을 알지 못했습니다. 교체 후

그래서 :

cout << "Some stuff " << some_var << " more stuff\n"; 

매력 같은

cout << "Some stuff " << some_var << " more stuff\n" << flush; 

작품

와.

0

호출 된 프로세스의 출력을 파일로 리디렉션하지 않고 프로그램에서 해당 파일을 읽지 않는 이유는 무엇입니까? 자식 프로세스가 출력을 잘 플러시하면 꽤 좋은 응답을 얻습니다.

+0

프로세스가 완료된 후에도 출력 파일이 작성되었습니다. –