I need to read output from native C++ console application in my C++/.NET. There are many articles about this, but most wait until the process ends to read the output, which I don't want to, I need to read it immediately after it is "cout-ed" by the process (and don't want to block the GUI while doing so, but that's something I can do on my own). I tried two ways. One:
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();
And the handler:
System::Void GUI::Form1::consoleHandler( System::Object^ sendingProcess, System::Diagnostics::DataReceivedEventArgs^ outLine ){
GUI::Form1::contentForConsole += outLine->Data + "\n";
}
But debugger confirmed it is called only after the process is finished.
On my second attempt I tried to create custom watching thread:
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);
And the thread function, it waits on the ReadLine function until the process finishes:
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
}
}
The process executable ouputs many lines of text over time span ranging from several seconds to several minutes (depending on the actual task).