need help understanding this code snippet

G

Guest

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{}
}
}
 
J

Jon Skeet [C# MVP]

mb said:
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();

It's not doing it all on one line - and it's not actually working,
either.

private ArrayList scannedSystems; // Declaring the ArrayList

Presumably somewhere it's actually *instantiating* the ArrayList too...

// Add an element to the list
scannedSystems.Add(new ScanPorts(ipAddress, port);

// Create a new thread which will run the systemScan method
// of the first element of the list (this is why there's a bug - it
// should be the *last* element of the list)
Thread scanThread = new Thread ( new ThreadStart
(((ScanPorts)scannedSystems[0]).systemScan));

// Start the new thread
scanThread.Start();

Mind you, it doesn't seem to actually do anything in terms of results,
either - there's nothing in ScanPorts to record whether or not it
managed to connect.
 
G

Guest

mb said:
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/d...y/en-us/csref/html/vcwlkthreadingtutorial.asp

More about delegates at:

http://msdn.microsoft.com/library/d...y/en-us/csref/html/vcwlkthreadingtutorial.asp

Hope it will help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top