Click here to Skip to main content
12,073,837 members (64,138 online)
Rate this:
 
Please Sign up or sign in to vote.
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
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 7-Jan-13 7:19am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You just need to create threadsafe calls:
How to: Make Thread-Safe Calls to Windows Forms Controls[^]
  Permalink  
Comments
leprechauny 7-Jan-13 13:32pm
   
Seems fair. Is this done by using a delegate?
   
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
   
Sure, a 5.
—SA
Marcus Kramer 8-Jan-13 10:24am
   
Thanks, Sergey
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Yes, you are write.

this cross domain error will occur when the following lines execute
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

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
bgWorker.WorkerReportsProgress = true;

for more information regarding the background worker please visit
http://msdn.microsoft.com/en-us/library/4852et58.aspx[^]
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100