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[];
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[
^]