I have list of IP and servers in my host file, which I am viewing in ListView control for better UI and I wrote a code snippet which will ping each server and if its pass it highlights the particular as green and if fails it highlight it with Red. Now whenever counter reaches 15 or 15+ it throws exeception, can any body help me on this.
There are around 42 server list which will appear we we scroll the listview control.
I guess at once only less than >15 items are displayed in screen may be this could be the reason.
private void pingAllIPinOneShot()
{
try
{
PingTest C_ping = new PingTest();
int i = 0;
string[] lines = File.ReadAllLines(_hostFilePath, Encoding.UTF8);
foreach (var line in lines)
{
i = i + 1;
if (line != " ")
{
string strResult = Regex.Replace(line, @"\s+", " ");
if (!strResult.Contains("#"))
{
string IPval = Before(strResult, " "); // to get value before sapce (i.e IP)
string serverVal = After(strResult, " ");// to get value before sapce (i.e server address)
string stat = PingAddr.GetPingAddr(serverVal);
if (stat != "Fail")
{
ListViewItem row = DataList.ItemContainerGenerator.ContainerFromIndex(i-1) as ListViewItem;
row.Background = Brushes.GreenYellow; //code fails here if 16th counter hit here
}
else
{
ListViewItem row = DataList.ItemContainerGenerator.ContainerFromIndex(i-1) as ListViewItem;
row.Background = Brushes.Red; //code fails here if 16th counter hit here
}
Thread.Sleep(1500); // delay to slow down the spreed
}
}
}
}
catch(Exception EX)
{
MessageBox.Show(EX.Message);
}
}