我遇到了一个奇怪的问题。我使用Visual Studio 2012/13在C#中编写了一个Windows窗体应用程序。我的应用程序打开一个控制台应用程序,并在运行时捕获它的标准和错误输出,然后将该输出打印到Windows窗体中的文本框。它过去工作得很好,但我必须改变一些无意中导致此功能停止工作的东西。
它现在仅在控制台应用程序完成并退出后,在Windows窗体中打印控制台应用程序的输出。我现在一直在反对这一点,我不知所措。我从Visual Studio 2012升级到2013,但项目没有改变,升级或类似的东西,所以我不相信这是罪魁祸首。任何帮助将不胜感激!
我相信我已经设置了正确的代码来捕获打印时的输出(无论如何这都用于工作):
transporterProcess.StartInfo.ErrorDialog = true;
transporterProcess.StartInfo.UseShellExecute = false;
transporterProcess.StartInfo.CreateNoWindow = true;
transporterProcess.StartInfo.RedirectStandardOutput = true;
transporterProcess.StartInfo.RedirectStandardError = true;
transporterProcess.EnableRaisingEvents = true;
transporterProcess.OutputDataReceived += new DataReceivedEventHandler(printOutput);
transporterProcess.ErrorDataReceived += new DataReceivedEventHandler(printError);
transporterProcess.Exited += new EventHandler(process_Exited);
...
transporterProcess.Start();
transporterProcess.BeginOutputReadLine();
transporterProcess.BeginErrorReadLine();
private void printOutput(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{ // output is a global StringBuilder object
output.Clear();
output.Append(outLine.Data + Environment.NewLine);
string update = output.ToString();
updateTextfromStandardOutput = new Thread(new ThreadStart(() => this.ThreadProcSafeStandardOutput(update)));
this.updateTextfromStandardOutput.Start();
}
}
private void printError(object sendingProcess,
DataReceivedEventArgs outLine)
{
// pretty much identical to the above code
}
private void ThreadProcSafeStandardOutput(string update)
{
this.setText(update);
}
private void ThreadProcSafeStandardError(string update)
{
this.setText(update);
}
private void setText(string text)
{
if (tbxOutput.InvokeRequired || listViewFilesToTransfer.InvokeRequired)
{
setTextCallback d = new setTextCallback(setText);
tbxOutput.Invoke(d, new object[] { text });
}
else
{
...
提前致谢!
答案 0 :(得分:0)
您需要设置EnableRaisingEvents
true
transporterProcess.EnableRaisingEvents = true;
答案 1 :(得分:0)
我将输出刷新添加到被调用的应用程序,这解决了@HansPassant提出的问题,虽然感觉有点像绷带,但至少它有效。