473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

need help understanding this code snippet

mb
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{}
}
}
Nov 17 '05 #1
2 1236
mb <mb@discussions.microsoft.com> 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();


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
"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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
0
by: VP | last post by:
G'day folks, well i am attempting to get an understanding on how to create the menuitems in a context menu on the fly. So far I have managed to actually achieve the menu items being created for...
4
by: David | last post by:
I have a problem that just cropped up with using an ImageList in my project I am using VB .NET 200 Problem: I have existing Form with 2 Image List controls. ImageList16 (for 16x16 Images) and...
7
by: Ronald S. Cook | last post by:
Thanks for reading this... Our current Win app employs a ListView control wherein the tag for an item is, for example, "ContentManager.DataModel.PowerPointContent". The following code exists...
6
by: Mark C | last post by:
Hi I am in progress of developing my own developers community site for practise tests. I would just like any type of feedback or opinions site is http://www.quiznetonline.com Thanks in...
1
by: TopherB | last post by:
Hi, First let me say that my knowledge of HTML and Javascript is fairly limited. But I am stuck in a situation of trying to adapt a website's shopping cart to a new one. Here's the problem, the...
6
by: DancingDave | last post by:
Can you help me understand why the following doesn't produce a recordset with just one record per Name ID? And also show me how to change it so it does? Thanks. SELECT ., Max(.Code) AS MaxOfCode,...
8
by: Mike Silva | last post by:
Hello all, I'm a longtime programmer (embedded with a smattering of desktop stuff as well) who knows very little about web programming. Right now I am developing the prototype of a multi-user...
2
Dormilich
by: Dormilich | last post by:
Hi, I'm testing my classes for a web page and I stumble upon an error I don't have a clue what it means: Error: Fatal error: Can't use method return value in write context in "output.php" on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.