Question

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));
            }
        }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top