i have this method to send ping for a duration.
public async Task<bool> TryToConnect(bool firstTime, int second)
{
var valueToReturne = false;
if (firstTime)
await Task.Delay(1200);
var ct = new CancellationTokenSource(TimeSpan.FromSeconds(second)).Token;
var pingSender = new Ping();
PingReply result;
try
{
while (!ct.IsCancellationRequested)
{
result = await pingSender.SendPingAsync("www.google.com", 100);//null exeption
if (result.Status != IPStatus.Success)
continue;
valueToReturne = true;
break;
}
}
catch (Exception)
{
valueToReturne = await TryToConnect(false, second / 2);
}
return valueToReturne;
}
For reasons I do not know. the
result = await pingSender.SendPingAsync("www.google.com", 100);
command sometimes that there is no internet access going through a null exception and the method going to the catch block befor ct ends.but i want to execute the
result = await pingSender.SendPingAsync("www.google.com", 100);
command under any circumstances for ct duration.