Hi I'm trying to create a simple program that pings everything on our network over a minute and changes the UI based on the results of that ping.
I have a background worker that sets the variables and pings the appropriate IP address.
This is contained within a while loop that waits for ping
to be true
. Within this loop is a For Loop that takes in user input to ping that specific amount of times.
The for loop worked fine, and in debugging I can see i
incrementing as usual.
I have a break point on the for loop line itself as well as the curly brace after it. If the user input is 2, it will run through twice, then the i
variable explicitly changes to 0
and then reaches the if()
statement
This was all working perfectly until I added some code within the nested for loops. But I cannot see anything that would be changing i
Here are some pictures showing what I mean
The loop has ran twice, and as such i
is equal to 2. I then continue once more to the curly brace and i
is now 0 and so doesn't break the loops.
Here's the entire method: ...
private async void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// variables
bool ping = false;
int i;
// Create ping objects
// TODO read these variables in from the config screen and create a class to handle it (populatePing)
// Ping 2
ping0.SetFqdn("##");
ping0.SetIpAddress("##");
ping0.SetFriendlyName("##");
// Ping 1
ping1.SetFqdn("##");
ping1.SetIpAddress("##");
ping1.SetFriendlyName("##");
// Create file (needs updating to utilise wildcards)
using StreamWriter file = new("C:/Users/##/AppData/Roaming/log.txt", append: true);
try
{
// Create background worker
BackgroundWorker worker = (BackgroundWorker)sender;
while (!ping)
{
// Write to log file
await file.WriteAsync("Starting job...\n");
await file.WriteAsync("Requested amount of pings: " count "\n");
// Create date object for logs
DateTime localDate = DateTime.Now;
for (i = 0; i < count; i )
{
// if count has reached the user input limit, break the loop
if (i == count)
{
ping = true;
}
// Create ping objects
System.Net.NetworkInformation.Ping pinger = new System.Net.NetworkInformation.Ping();
PingReply ip0_ping, ip1_ping;
try
{
if(!ping0.Equals(null))
{
try
{
// Send ping to specified IP address
ip0_ping = pinger.Send(ping0.GetIpAddress());
// Write log file
await file.WriteLineAsync(localDate.TimeOfDay " | Friendly Name" ping0.GetFriendlyName() ": Ping: " ip0_ping.Address " status: " ip0_ping.Status ". time: " ip0_ping.RoundtripTime "ms");
// Successful ping has been sent
if(ip0_ping.Status.ToString().Contains("Success"))
{
ping0.SetSuccessfulPings(1);
} else // Unsuccessful ping has been sent
{
ping0.SetFailedPings(1);
}
}
catch(Exception d)
{
Debug.WriteLine(d.ToString());
}
} else
{
Debug.WriteLine("ERROR: ping0 is not being populated correctly");
}
if(!ping1.Equals(null))
{
try
{
// Send ping to specified IP address
ip1_ping = pinger.Send(ping1.GetIpAddress());
// Write log file
await file.WriteLineAsync(localDate.TimeOfDay " | Friendly Name" ping1.GetFriendlyName() ": Ping: " ip1_ping.Address " status: " ip1_ping.Status ". time: " ip1_ping.RoundtripTime "ms");
// Successful ping has been sent
if (ip1_ping.Status.ToString().Contains("Success"))
{
ping1.SetSuccessfulPings(1);
}
else // Unsuccessful ping has been sent
{
ping1.SetFailedPings(1);
}
}
catch(Exception d)
{
Debug.WriteLine(d.ToString());
}
}
// wait one second
wait(1000);
}
catch (Exception b)
{
Debug.WriteLine(b.ToString());
}
}
}
} catch (Exception a)
{
Debug.WriteLine(a.ToString());
}
}
...
Sorry if this is really obvious but I'm really stumped
CodePudding user response:
The method will not exit since the for loop is not entered if i == count since the condition section of your for loop evaluates if i < count.
See the following link under the for loop section: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements
The condition section that determines if the next iteration in the loop should be executed. If it evaluates to true or is not present, the next iteration is executed; otherwise, the loop is exited. The condition section must be a Boolean expression.
and also
The iterator section that defines what happens after each execution of the body of the loop.