C#处理标准输出延迟
C# process standard output delay
从 C# 表单我是 运行 a process with start info similar to Redirect console output to textbox in separate program and C# get process output while running, the process runs correctly however the output takes a long time to appear in the DataReceived event.
我想在进程生成文本后立即看到它;根据 Process standard output cannot be captured?(第一条评论),我需要等到 2 到 4 kb 的缓冲区填充后才能触发事件。
根据要求,这是代码:
void pcs_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
textBox1.BeginInvoke((Action)delegate { textBox1.AppendText(text + "\n"); });
}
private void LER_Go_Click(object sender, EventArgs e)
{
// variables LiDARExtRep contains the full path to an executable file
// that runs in DOS and produces verbose output.
// LER_Path.Text is the parameter passed to LiDARExtRep (only one arg for this example)
ProcessStartInfo pStartInfo = new ProcessStartInfo(LiDARExtRep, LER_Path.Text);
pStartInfo.UseShellExecute = false;
pStartInfo.ErrorDialog = false;
pStartInfo.RedirectStandardError = true;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.CreateNoWindow = true;
System.Diagnostics.Process pcs = new System.Diagnostics.Process();
pcs.StartInfo = pStartInfo;
bool pStarted = pcs.Start();
pcs.OutputDataReceived += new DataReceivedEventHandler(pcs_OutputDataReceived);
pcs.BeginOutputReadLine();
pcs.WaitForExit();
}
我看不出它有什么特别之处,它与我引用的示例完全相同...构造函数中的简单 "Dir","/b/s"
应该会产生相同的结果。
有没有办法将缓冲区减少到几个字节,或者有更好的方法来执行命令行工具并接收输出 'real time'?
背景:我用C++写了一些命令行程序,效果很好很好,但年轻一代似乎害怕DOS,所以我正在创建一个表单(GUI)收集这些工具的参数,因为它似乎比尝试在 C++ 中的每个程序上放置一个 GUI 少 很多 工作。如果我无法获得实时响应,我将不得不 UseShellExecute = true;
并显示命令 window.
从 C# 表单我是 运行 a process with start info similar to Redirect console output to textbox in separate program and C# get process output while running, the process runs correctly however the output takes a long time to appear in the DataReceived event.
我想在进程生成文本后立即看到它;根据 Process standard output cannot be captured?(第一条评论),我需要等到 2 到 4 kb 的缓冲区填充后才能触发事件。
根据要求,这是代码:
void pcs_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
textBox1.BeginInvoke((Action)delegate { textBox1.AppendText(text + "\n"); });
}
private void LER_Go_Click(object sender, EventArgs e)
{
// variables LiDARExtRep contains the full path to an executable file
// that runs in DOS and produces verbose output.
// LER_Path.Text is the parameter passed to LiDARExtRep (only one arg for this example)
ProcessStartInfo pStartInfo = new ProcessStartInfo(LiDARExtRep, LER_Path.Text);
pStartInfo.UseShellExecute = false;
pStartInfo.ErrorDialog = false;
pStartInfo.RedirectStandardError = true;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.CreateNoWindow = true;
System.Diagnostics.Process pcs = new System.Diagnostics.Process();
pcs.StartInfo = pStartInfo;
bool pStarted = pcs.Start();
pcs.OutputDataReceived += new DataReceivedEventHandler(pcs_OutputDataReceived);
pcs.BeginOutputReadLine();
pcs.WaitForExit();
}
我看不出它有什么特别之处,它与我引用的示例完全相同...构造函数中的简单 "Dir","/b/s"
应该会产生相同的结果。
有没有办法将缓冲区减少到几个字节,或者有更好的方法来执行命令行工具并接收输出 'real time'?
背景:我用C++写了一些命令行程序,效果很好很好,但年轻一代似乎害怕DOS,所以我正在创建一个表单(GUI)收集这些工具的参数,因为它似乎比尝试在 C++ 中的每个程序上放置一个 GUI 少 很多 工作。如果我无法获得实时响应,我将不得不 UseShellExecute = true;
并显示命令 window.