Reputation: 6480
I am working on pinging app in which I have a progress bar which displays progress of pinging of range IPs.
This is how I calculate the range of IPs to set as maximum value for the progress bar:
pbProgress.Maximum = 1 + (IPAddress.NetworkToHostOrder
(BitConverter.ToInt32(IPAddress.Parse(txtTo.Text).GetAddressBytes(), 0)) -
IPAddress.NetworkToHostOrder(BitConverter.ToInt32
(IPAddress.Parse(txtFrom.Text).GetAddressBytes(), 0)));
The problem here is that I put range starting from 0.0.0.0 to 1.0.0.0 it takes an awful amount of time to calculate the range, therefore passing the value to the progress bar maximum value. Is there any better solution to calculate the number of IPs quicker?
I could always put a limit of IP addresses to ping for example to 10k addresses, but I just don't want to limit any functionality of this program.
Any ideas?
Upvotes: 0
Views: 409
Reputation: 86718
Your problem is that you're creating some data structure, like a list or array, containing each of the IP addresses. All you need to do is iterate over them with something like a for
loop so they are generated as needed rather than before you start.
Since you want to use multiple threads, try using something like Parallel.For
.
Upvotes: 1