Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
C#
for (int currPort = firstInterval; firstInterval <= lastInterval; currPort++)
            {
                TcpClient TcpScan = new TcpClient();
                try
                {
                    TcpScan.Connect(ipAddress, currPort);
                    resultTxt.AppendText("Port " + currPort + " open\r\n");
                }
                catch
                {
                    resultTxt.AppendText("Port " + currPort + " closed\r\n");
                }

                if (bgWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
            }


This code is running under
C#
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)

which isn't really working as I was hoping. It throws a cross-thread exception error. I'm not quite sure how to send the string into the result textbox. I'm thinking that it might be possible to solve by using the ProgressChanged event. Is that what I'm missing?
Posted

You just need to create threadsafe calls:
How to: Make Thread-Safe Calls to Windows Forms Controls[^]
 
Share this answer
 
Comments
leprechauny 7-Jan-13 13:32pm    
Seems fair. Is this done by using a delegate?
Sergey Alexandrovich Kryukov 7-Jan-13 18:35pm    
Sure. Delegate instances, to be more precise. It's a queue of delegate instances and data needed to call them on UI control, posted via Control.Invoke or Control.BeginInvoke.
—SA
Sergey Alexandrovich Kryukov 7-Jan-13 18:34pm    
Sure, a 5.
—SA
fjdiewornncalwe 8-Jan-13 10:24am    
Thanks, Sergey
Yes, you are write.

this cross domain error will occur when the following lines execute
C#
resultTxt.AppendText("Port " + currPort + " open\r\n");
resultTxt.AppendText("Port " + currPort + " closed\r\n");


That because, you are going to access the Main Thread control within the background thread.
Yes, your are write. the best way to use the ProgressChanged event.

Here are the code sample

C#
for (int currPort = firstInterval; firstInterval <= lastInterval; currPort++)
{
  TcpClient TcpScan = new TcpClient();
  try
  {
	TcpScan.Connect(ipAddress, currPort);
	bgWorker.ReportProgress(1, new string[] { currPort.toString() , "open" });                    
  }
  catch
  {                    
	bgWorker.ReportProgress(2, new string[] { currPort.toString() , "closed" });
  }

  if (bgWorker.CancellationPending)
  {
	e.Cancel = true;
	return;
  }
}

private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    var val = e.UserState as string[];
    //index 0 = port number, index 1 = status
     resultTxt.AppendText("Port " + val[0] + " " + val[1] + "\r\n");

}


please note you have to configure the background worker to report the progress as below
C#
bgWorker.WorkerReportsProgress = true;


for more information regarding the background worker please visit
http://msdn.microsoft.com/en-us/library/4852et58.aspx[^]
 
Share this answer
 
v2
Comments
leprechauny 7-Jan-13 13:31pm    
Yes, exactly. How am I going to go about to send the information from the background thread to the main thread using ProgressChanged event?
Tharaka MTR 7-Jan-13 14:03pm    
please see my answer, I have updated with code sample
leprechauny 7-Jan-13 14:19pm    
Ah, seems good. Seems to work, however the following

resultTxt.AppendText("Port " + val[0] + " " + val[1] + "\r\n");

throws a nullrefexception

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900