2009-05-01 87 views
0

我试图把从nmap标准输出到WPF窗口应用程序(正确的文本框)。我正在尝试使用Dispatcher.Invoke,但是当nmap进程启动时,一切都会冻结。当我在一个控制台应用程序(不包含Invoke)中尝试这个时,一切正常,我认为这是Invoke方法的问题。 Nmap本身正在工作,并完成它的工作,但在我的窗口没有任何反应。从不同的进程更新UI失败

下面是我使用的代码:

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)); 
      } 
     } 

回答

2

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

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

+0

使用BeginInvoke窗口在nmap进程结束后立即启动进程消失,并且依然没有在窗口中输出。 nmapOutput肯定是由UI线程创建的。 – anusiak 2009-05-02 02:58:37