Click here to Skip to main content
Rate this: bad
good
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 at 7-Jan-13 13:32pm
   
Seems fair. Is this done by using a delegate?
Sergey Alexandrovich Kryukov at 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 at 7-Jan-13 18:34pm
   
Sure, a 5.
—SA
Marcus Kramer at 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 at 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 at 7-Jan-13 14:03pm
   
please see my answer, I have updated with code sample
leprechauny at 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
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
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