From my WPF, i need to verify internet connection constantly, connection availble set to gree, if no connection, set to red.
From my below code i have the following issue:
- if there is no internet connection or if i pull the rj45 lan cabel or if i put a static/random ip 192.168.192.90, 255.255.255.0, gateway:192.168.192.90, pref dns:8.8.8.8, the UI is not updating instead it throws various exceptions.
Could you please tell me where am i making the mistake?
private DispatcherTimer BackgroundAsyncTasksTimer;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BackgroundAsyncTasksTimer = new DispatcherTimer();
BackgroundAsyncTasksTimer.Interval = TimeSpan.FromMilliseconds(2000);
BackgroundAsyncTasksTimer.Tick = BackgroundAsyncTasksTimer_Tick;
BackgroundAsyncTasksTimer.Start();
}
private async void BackgroundAsyncTasksTimer_Tick(object sender, object e)
{
try
{
bool tresult = await IsWebsiteUp_Ping("https://google.com");
if (tresult)
{
this.einternetcoxn.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#841c34");
}
else
{
this.einternetcoxn.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#00ff00");
}
}
catch(Exception es)
{
Console.WriteLine("Source :{0} ", es.ToString());
}
}
private async Task<bool> IsWebsiteUp_Ping(string url)
{
try
{
Ping ping = new Ping();
var hostName = new Uri(url).Host;
byte[] buffer = Encoding.ASCII.GetBytes("samplestring");
PingReply result = await ping.SendPingAsync(hostName, 500, buffer);
return result.Status == IPStatus.Success;
}
catch (System.Net.Sockets.SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " e.Source);
Console.WriteLine("Message : " e.Message);
return false;
}
}
Exceptions:
à System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
à System.Net.NetworkInformation.Ping.ContinueAsyncSend(Object state)
--- Fin de la trace de la pile d'exception interne ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
à Maintenance.MainWindow.<IsWebsiteUp_Ping>d__19.MoveNext() dans C:\Users\user1\source\repos\Maintenance\Maintenance\MainWindow.xaml.cs:ligne 252
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
à Maintenance.MainWindow.<BackgroundAsyncTasksTimer_Tick>d__14.MoveNext() dans C:\Users\\user1\source\source\repos\Maintenance\Maintenance\MainWindow.xaml.cs:ligne 152
Exception thrown: 'System.Net.NetworkInformation.PingException' in mscorlib.dll
Source :System.Net.NetworkInformation.PingException: Une exception s'est produite lors d'une demande PING. ---> System.Net.Sockets.SocketException: Hôte inconnu
à System.Net.Dns.GetAddrInfo(String name)
à System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)
à System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
à System.Net.NetworkInformation.Ping.ContinueAsyncSend(Object state)
--- Fin de la trace de la pile d'exception interne ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
à Maintenance.MainWindow.<IsWebsiteUp_Ping>d__19.MoveNext() dans C:\Users\\user1\source\source\repos\Maintenance\Maintenance\MainWindow.xaml.cs:ligne 252
--- Fin de la trace de la pile à partir de l'emplacement précédent au niveau duquel l'exception a été levée ---
à System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
à System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
à System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
I tried using the above code but i'm not able to capture or understand where am i doing wrong.
CodePudding user response:
According to the docs, SendPingAsync
may throw SocketException
or PingException
. It's throwing a PingException
and your code is catching a SocketException
.