2009-05-01 82 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