I have code that checks to see if it has the internet or not.
public static bool InternetIsConnected()
{
try
{
using (var client = new WebClient())
{
using (client.OpenRead("http://clients3.google.com/generate_204"))
{
return true;
}
}
}
catch
{
return false;
}
}
How to convert the code to async/await
?
Update:
Attempt 1: (Comment from @Virgilio Novic)
public async bool InternetIsConnected()
{
try
{
using (var client = new WebClient())
{
Uri uri = new Uri("http://clients3.google.com/generate_204");
using (await client.OpenReadAsync(uri))
{
return true;
}
}
}
catch
{
return false;
}
}
Code above gives error: "Can not wait for void".
Using the "Ping" class: (Functional)
private async Task<bool> InternetIsConnected()
{
Ping ping = new Ping();
try
{
await ping.SendPingAsync("google.com", 3000, new byte[32], new PingOptions(64, true));
return true;
}
catch (Exception)
{
return false;
}
}