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.