I am using this code.. calling ping_to_host() from a timer every 5 seconds and eveything is fine until it reachs the last row in the listview (let's say index=16).. it should then go back to index = 0 but it's failing with an invalid row index= 17.. but i don't have this crazy index 17 and i really don't know why it's adding one more index though i have mentioned:
for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
this is the code:
public void ping_to_host()
{
CheckForIllegalCrossThreadCalls = false;
listview_item = 0;
for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
{
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
Ping ping_sender = new Ping();
PingOptions ping_options = new PingOptions();
ping_options.DontFragment = true;
PingReply ping_reply = ping_sender.Send(lstHosts.Items[listview_item].SubItems[1].Text, 5000, buffer, ping_options);
if (ping_reply.Status == IPStatus.Success)
{
lstHosts.Items[listview_item].SubItems[0].Text = "Success";
lstHosts.Items[listview_item].SubItems[2].Text = ping_reply.RoundtripTime.ToString();
lstHosts.Items[listview_item].SubItems[3].Text = ping_reply.Options.Ttl.ToString();
lstHosts.Items[listview_item].SubItems[4].Text = "1";
lstHosts.Items[listview_item].Tag = "T";
lstHosts.Items[listview_item].BackColor = Color.Black;
}
else
{
switch (ping_reply.Status){
case IPStatus.BadDestination: ping_status = "Bad Destination"; break; case IPStatus.BadHeader: ping_status = "Bad Header"; break; case IPStatus.BadOption: ping_status = "Bad Option"; break; case IPStatus.BadRoute: ping_status = "Bad Route"; break; case IPStatus.DestinationHostUnreachable: ping_status = "Destination Host Unreachable"; break; case IPStatus.DestinationNetworkUnreachable: ping_status = "Destination Network Unreachable"; break; case IPStatus.DestinationPortUnreachable: ping_status = "Destination Port Unreachable"; break; case IPStatus.DestinationProhibited: ping_status = "Destination Prohibited"; break; // case IPStatus.DestinationProtocolUnreachable: ping_status = "Destination Protocol Unreachable"; break; case IPStatus.DestinationScopeMismatch: ping_status = "Destination Scope Mismatch"; break; case IPStatus.DestinationUnreachable: ping_status = "Destination Unreachable"; break; case IPStatus.HardwareError: ping_status = "Hardware Error"; break; case IPStatus.IcmpError: ping_status = "ICMP Error"; break; case IPStatus.NoResources: ping_status = "No Resources"; break; case IPStatus.PacketTooBig: ping_status = "Packet Too Big"; break; case IPStatus.ParameterProblem: ping_status = "Parameter Problem"; break; case IPStatus.SourceQuench: ping_status = "Source Quench"; break; case IPStatus.TimedOut: ping_status = "Timed Out"; break; case IPStatus.TimeExceeded: ping_status = "Time Exceeded"; break; case IPStatus.TtlExpired: ping_status = "Ttl Expired"; break; case IPStatus.TtlReassemblyTimeExceeded: ping_status = "Ttl Reassembly Time Exceeded"; break; case IPStatus.Unknown: ping_status = "Unknown"; break; case IPStatus.UnrecognizedNextHeader: ping_status = "Unrecognized Next Header"; break; default: ping_status = "Unknow"; break;}
lstHosts.Items[listview_item].SubItems[0].Text = ping_status;
lstHosts.Items[listview_item].SubItems[2].Text =
"";lstHosts.Items[listview_item].SubItems[3].Text =
"";lstHosts.Items[listview_item].SubItems[4].Text =
"0";lstHosts.Items[listview_item].Tag =
"F";lstHosts.Items[listview_item].BackColor =
Color.Red;}
lstHosts.ListViewItemSorter =
new ListViewItemComparer(4);toolStatus.Text =
"Idle - " + DateTime.Now.ToLongTimeString();}
// lstHosts.EndUpdate();}

plz HELP!! it's making me crazy!!
Balsoft
lstHosts.ListViewItemSorter = new ListViewItemComparer(4);
Do you really want to be changing the sorter every time through the loop Perhaps this line should come after the end of the for loop.
Hope this (makes sense and) helps,Doug
Linlin425872
MaggieChan
same error...
InvalidArgument=Value of '17' is not valid for 'index'.
Parameter name: index
nzmike
still the same problem... this is the (I just added few records in the listView as well)...
InvalidArgument=Value of '31' is not valid for 'index'.
Parameter name: index
NOTE: Itried Count - 2, Count - 3 and Count - 5
MrEezy
SCarmeli
When you increment i and increase the index, before it starts over to index 0, it is most likely incrementing i an extra time. So look anywhere in your code to make sure that i is not incremented an extra time and that that extra incrementation does not effect how your indexes are selected.
EDIT: The problem lies within:
for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
So play around with that line of code until you can get it to work. Have you tried making 1stHosts.Items.Count -1 a -2 or -3
PatrickDonohue
Jkumar
well, i thought soring at the end of the loop but it's customers links from my server and each minute is critical for me.. the 17 items are just dummy but the real number is more than 500 so i won't l know if item 1 failed until it comes back again after 499 items..
koad
firstly, never set the CheckForIllegalCrossThreadCalls to false, its bad practice. There is a reason why it was implemented and there are a good few solutions here on how to use it properly, invoking the UI Control
secondly, I know you have done this:
for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
try this
for (listview_item = 0; listview_item < lstHosts.Items.Count; listview_item++)
however can you try this and see what happens