我正在为第三方控制台应用程序编写一个 GUI,我希望它捕获控制台窗口的输出并将其添加到 GUI 中的文本框。问题是,当我尝试重定向目标进程的输出流时,目标进程崩溃并显示以下错误消息:
CTextConsoleWin32::GetLine: !GetNumberOfConsoleInputEvents
触发此错误的代码如下:
// Called once after the application has initialized.
private void StartServer()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = srcdsExeFile;
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
serverProcess = Process.Start(processStartInfo);
serverProcess.EnableRaisingEvents = true;
serverProcess.Exited += new EventHandler(Server_Exited);
serverProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
serverProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
serverProcess.BeginOutputReadLine();
serverProcess.BeginErrorReadLine();
}
private void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.Output.WriteLine("\n\nServer Error: " + e.Data + "\n\n");
}
private void ServerProcess_OutputDataReceived(object sender, …Run Code Online (Sandbox Code Playgroud)