我正在尝试将来自 nmap 的标准输出放入 WPF 窗口应用程序(确切地说是文本框)。我正在尝试使用 Dispatcher.Invoke 但是当 nmap 进程启动时,一切都冻结了。当我在控制台应用程序(不带 Invoke)中尝试此操作时,一切正常,我认为这是 Invoke 方法的问题。Nmap 本身正在工作,并且正在完成它的工作,但我的窗口中没有任何响应。
这是我正在使用的代码:
Process nmap = new Process();
nmap.StartInfo.FileName = Properties.Settings.Default.NmapResidentialPath;
nmap.StartInfo.Arguments = arguments.ToString();
nmap.StartInfo.UseShellExecute = false;
nmap.StartInfo.RedirectStandardOutput = true;
nmap.OutputDataReceived += new DataReceivedEventHandler(nmap_OutputDataReceived);
nmap.Start();
nmap.BeginOutputReadLine();
nmap.WaitForExit();
nmap.Close();
和事件处理方法:
void nmap_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data))
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => nmapOutput.Text += "\n" + e.Data));
}
}