views:

69

answers:

2

I want to write network address to my listview, in a range like 192.168.0.0 -192.168.255.255 and I wrote a thread application but when I run this app, all threads are trying to add addresses to listview, does it has a simple solution?

here is my code:

namespace ListNetworkComputers
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        const int step = 16777216;
        int threadCount = 1;

        private void frmMain_Load(object sender, EventArgs e)
        {
            ıpAddressControl1.Text = "192.168.0.0";
            ıpAddressControl2.Text = "192.168.255.255";
        }

        private void btnShowPcc_Click(object sender, EventArgs e)
        {

            threadCount = Convert.ToInt32(nudThreads.Value);


            Thread[] threads = new Thread[threadCount];

            for (int i = 0; i < threadCount; i++)
            {
                threads[i] = new Thread(new ThreadStart(getPerformance));
                threads[i].Name = string.Format(i.ToString());
            }

            foreach (Thread t in threads)
            {
                t.Start();
            }
        }

        private void getPerformance()
        {
            uint startIntAdress, endIntAdress;

            startIntAdress = BitConverter.ToUInt32(IPAddress.Parse(ıpAddressControl1.Text).GetAddressBytes(), 0);
            endIntAdress = BitConverter.ToUInt32(IPAddress.Parse(ıpAddressControl2.Text).GetAddressBytes(), 0);

            for (uint i = startIntAdress; i < endIntAdress; i = i + step)
            {
                string ipAddress = new IPAddress(BitConverter.GetBytes(i)).ToString();
                lbNetworkComputers.Items.Add(ipAddress);
            }
        }
    }
}

And an another problem is, my step method (increaseing adresses as 16777216 ...) isnt working healthy. it goes 192.168.0.0 to 192.168.0.255 but doesnt go on after that.

+1  A: 

Each thread is running exactly the same code as your loop over the IP addresses is inside the method passed to each thread.

You should pass different start and end addresses into each thread.

You'll also have problems with the threads accessing the UI.

From the code you've posted I'm not sure this really needs to be threaded.

ChrisF
yes you are right, this process doesnt need to threads I will get performance counters for each machine, so this is a simple practice for me.
Rapunzo
@Rapunzo - that's OK - I thought I might have been missing something.
ChrisF
+3  A: 

Because they get same startIntAdress and endIntAdress. Split the range evenly for all threads.

It should be like this:

Thread 1 starts at 192.168.0.0 and checks 32 addresses
Thread 2 at 192.168.0.31 and checks 32,
Thread 3 at 192.168.0.63 and checks 32,
etc
Im0rtality
Or, better yet, have the individual threads pull the next address to check from a centralized function.
tdammers
Do I have to give differend jobs manualy? forexample can threads look only never looked addresses?
Rapunzo
@Rapunzo: You could somehow pass parameter (thread `t` index from `foreach`) to thread when it starts, so thread could calculate starting address like this (last byte of IP in this case): `part4 = (255 / threadCount) * threadIndex;` note: threadCount should be value from 1,2,4,8,16,32,64,... to get reminder == 0
Im0rtality