当我从代码调用外部exe并将输出重定向到我的UI richtextbox后,UI挂起并且没有在屏幕上显示输出,它慢慢启动延迟UI屏幕上的输出,任何人都知道为什么这是发生了。这是我的代码
private void RunExe(string arg)
{
var cmdStartInfo = new ProcessStartInfo
{
Arguments = arg,
FileName = System.IO.Path.Combine(Application.StartupPath, @"Demo.exe"),
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
_cmd = new Process {StartInfo = cmdStartInfo};
_cmd.OutputDataReceived +=new DataReceivedEventHandler(OutputHandler);
_cmd.Start();
ProcessId = _cmd.Id;
_enableButton = new Task(_cmd_Exited);
_enableButton.Start();
MyTabControl.Visibility = Visibility.Visible;
if (!MyTabControl.Items.Contains(ConsoleTabItem))
{
MyTabControl.Items.Add(ConsoleTabItem);
}
txtConsoleOutput.Document.Blocks.Clear();
ConsoleTabItem.Focus();
ConsoleTabItem.Visibility = Visibility.Visible;
ConsoleTabItem.IsEnabled = false;
_cmd.BeginOutputReadLine();
LoadResults();
}
输出处理程序
private void OutputHandler(object sender, DataReceivedEventArgs e)
{
try
{
if (e.Data != null)
{
string line;
Dispatcher.Invoke(() =>
{
line = e.Data;
if (line != null)
{
txtConsoleOutput.AppendText(line + "\n");
ConsoleScrollerView.ScrollToBottom();
}
});
}
}
catch (NullReferenceException ex)
{
Console.WriteLine(ex);
}
}
我真的很感激任何帮助..谢谢