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