【发布时间】:2023-03-21 10:44:01
【问题描述】:
我需要在我的 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
}
}
进程可执行文件输出多行文本,时间跨度从几秒到几分钟不等(取决于实际任务)。
【问题讨论】:
-
我也对这个感兴趣;我们的商店也有类似的问题,但不是在关键位置。