views:

118

answers:

2

hi this is my multi thread codes it works fine

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace searchIpAdresses
{
    public partial class frmSearchIpRange : Form
    {
        public frmSearchIpRange()
        {
            InitializeComponent();
        }

        int intStartIp, intEndIp;

        private void frmSearchIpRange_Load(object sender, EventArgs e)
        {
            startIp.Text = "0.0.0.0";
            endIp.Text = "0.1.0.0";
            nudThreads.Value = 1;
        }

        private bool calcIpAddressRange()
        {
            intStartIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(startIp.Text).GetAddressBytes(), 0));
            intEndIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(endIp.Text).GetAddressBytes(), 0));
            if (intStartIp>intEndIp)
            {
                MessageBox.Show("End Ip must be bigger than Begin Ip!");
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (calcIpAddressRange())
            {
                int 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();
                }

                //for (int i = 0; i < nudThreads.Value; i++)
                //{
                //    if (threads[i].IsAlive)
                //        threads[i].Abort();
                //}
            }

        }

        private void getPerformance()//int sampleStartIp, int sampleEndIp
        {
            if (calcIpAddressRange())
            {
                lbAddress.Items.Clear();
                progressBar1.Maximum = intEndIp - intStartIp;
                int i;
                for (i = intStartIp; i < intEndIp; i++)
                {
                    string ipAddress = new IPAddress(BitConverter.GetBytes(IPAddress.NetworkToHostOrder(i))).ToString();

                    progressBar1.Value++;
                    lbAddress.Items.Add(ipAddress);
                }
                MessageBox.Show(i.ToString() + " addresses added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                progressBar1.Value = 0;
            }
        }
    }
}

but I want to split jobs in getPerformance function. so I have to use it like

threads[i] = new Thread(new ThreadStart(getPerformance(sampleStart, sampleEnd)))

etc in this case visual studio gives me "Method name expected" error. what is the solution ?

edit : I tried this but I couldnt adapted it to array format work @Henk Holterman

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());

                    Thread t = new Thread(() => getPerformance(intStartIp, intEndIp));
                }

                foreach (Thread t in threads)
                {
                    t.Start();
                }
+3  A: 

1) You probably should be using the Threadpool or Tasks (Fx4).

2) When you can use delegates lamba'a (.NET 3 and later)

  int a=1, b=2;
  Thread t = new Thread(() => Worker(a, b));

For older versions you can use ParameterizedThreadStart and a helper object to hold Start and End.


Edit to adapt your loop:

for (int i = 0; i < threadCount; i++)
{
   // some code to update intStartIp, intEndIp 

   //threads[i] = new Thread(new ThreadStart(getPerformance));                  
   threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp));
   threads[i].Name = string.Format(i.ToString());   
}
Henk Holterman
can I use this for multithread? because this "t" variable isn't an array type
Rapunzo
Yes, you can adapt this to your code. `t` is of type Thread.
Henk Holterman
@Rapunzo: I Can't read that, add a section to your question.
Henk Holterman
Ok @ Henk Holterman I added :)
Rapunzo
+3  A: 

You can use ParameterizedThreadStart for passing object into the called method

DixonD