views:

247

answers:

1

I'm trying to put standard output from nmap into WPF window application (textbox exactly). I'm trying to use Dispatcher.Invoke but when nmap process starts everything just freezes. When I tried this in a console application (w/o Invoke), everything worked just fine, I think it's a problem with the Invoke method. Nmap itself is working, and finishing it work, but there is no response in my window.

Here's the code I'm using:

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

And the event handler method:

void nmap_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Data))
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => nmapOutput.Text += "\n" + e.Data));
            }
        }
+1  A: 

This may be caused by various reasons. First, Ensure that nmapOutput control is created on UI Thread. Second, Dispatcher.Invoke may cause UI Thread deadlock (and it probably is in your case).

Always call Dispatcher.CheckAccess() before calling Invoke, or use BeginInvoke to perform this operation in async manner.

jarek
With BeginInvoke window that starts the process dissapears right after the nmap process ends, and still I have no output in my window at all.nmapOutput is created by UI thread for sure.
anusiak
Do you start nmap process in background thread?If not than nmap.WaitForExit() will block UI thread and freeze the window.
jarek