我有一个控制台应用程序,我想在 ac# 应用程序中处理标准输出。
基本上我已经设法用这段代码做到了:
Process ProcessObj = new Process();
ProcessObj.StartInfo.WorkingDirectory = WorkingPath;
ProcessObj.StartInfo.FileName = ApplicationPath;
ProcessObj.StartInfo.Arguments = ApplicationArguments;
ProcessObj.StartInfo.UseShellExecute = false;
ProcessObj.StartInfo.CreateNoWindow = true;
ProcessObj.StartInfo.RedirectStandardOutput = true;
// Start the process
ProcessObj.Start();
// loop through until the job is done
bool stopper = false;
while (!stopper)
{
stopper = ProcessObj.WaitForExit(100);
string line = null;
// handle normal outputs (loop through the lines)
while (true)
{
line = ProcessObj.StandardOutput.ReadLine();
if (line == null)
break;
Logger.Trace("Out: \"" + line + "\"");
}
}
当该过程仅运行几秒钟时,看起来整个过程都在正常工作。当我更改控制台应用程序的配置以计算更多时,进程运行了数小时。在这个时候,我的 C# 应用程序没有得到控制台应用程序的响应。由于控制台应用程序被隐藏,因此看起来应用程序卡住了,但事实并非如此。它已经在后台运行,并且似乎所有 std 输出仅在控制台应用程序完成执行时才通过管道传输到我的 c# 应用程序。所以问题是,我没有在我的 c# 应用程序中看到标准输出行。控制台应用程序完成后数小时后将刷新。
有没有办法刷新这个标准输出重定向?任何人都知道为什么这不像我想要的那样工作?
PS:当我在普通 cmd 窗口中独立执行控制台应用程序时,输出会实时显示,没有任何问题。
请帮忙。