"mb" wrote:
got the following code snippet that i've modified off the internet to work -
but damn if i know how and i dont like that. im having trouble with the
following thread declaration that creates the thread, calls the ScanPorts
method in the ScanPorts class and somehow passes in the array list
scannedSystems. i'm stumped - how does it do all that on one line? thanks
all.
private ArrayList scannedSystems;
scannedSystems.Add(new ScanPorts(ipAddress, port);
Thread scanThread = new Thread ( new ThreadStart
(((ScanPorts)scannedSystems[0]).systemScan));
scanThread.Start();
public class ScanPorts
{
private string address = "";
private int scannedPort;
public ScanPorts(string ipAddress, int port)
{
string address = ipAddress;
int scannedPort = port;
}
public void systemScan()
{
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(address, scannedPort);
}
catch{}
}
}
I guess you wanted to understand this part:
<CODE>
scannedSystems.Add(new ScanPorts(ipAddress, port));
Thread scanThread = new Thread ( new
ThreadStart(((ScanPorts)scannedSystems[0]).systemScan));
scanThread.Start();
</CODE>
As other things are pretty simple to understand.
Ok i will try to explain each line one by one:
scannedSystems.Add(new ScanPorts(ipAddress, port));
Here a new ScanPorts object is created and it has been added to the
scannedSystems ArrayList.
Thread scanThread = new Thread ( new
ThreadStart(((ScanPorts)scannedSystems[0]).systemScan));
Here first we are extracting the previously added ScanPorts object from the
scannedSystems ArrayList and casting it to proper type of ScanPorts as
ArrayList stores the data as basic object type. Next we are created a
ThreadStart delegate for ScanPorts class method systemScan. And then passing
this delegate we are creating a new Thread.
scanThread.Start();
Now with this line the thread execution is started. Hence the thread will be
spawned and it will start executing the ScanPorts syatemScan method.
More about Threads can be found at:
http://msdn.microsoft.com/library/de...ngtutorial.asp
More about delegates at:
http://msdn.microsoft.com/library/de...ngtutorial.asp
Hope it will help.
--
Cheers,
Rahul Anand