0

我正在尝试将来自 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));
            }
        }
4

1 回答 1

2

这可能是由各种原因造成的。首先,确保在 UI 线程上创建了 nmapOutput 控件。其次, Dispatcher.Invoke 可能会导致 UI 线程死锁(这可能是您的情况)。

在调用 Invoke 之前始终调用 Dispatcher.CheckAccess(),或使用 BeginInvoke 以异步方式执行此操作。

于 2009-05-01T19:52:58.683 回答