Ping verify internet connection blocks UI

Kran2022 381 Reputation points
2022-11-24T14:58:15.543+00:00

Hi All:

In my WPF app, during the app launch detects static ip address as bleow, the UI is freezing. Could you please someone tell me what is the mistake? thanks

My app should verify if there is an internet connection in the system for every x seconds, Yes: Green, No:Red color

Static IP: 192.168.192.91, Subnet:255.255.255.0 Def Gateway:192.168.192.90, DNS: 12.12.8.8

   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)  
                {  
                    
                    bool details = await IsWebsiteUp_Ping("https://stackoverflow.com/");  
                    if (details)  
                    {  
                        einternetcoxn.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#00ff00");   
                    }  
                    else  
                    {  
                        einternetcoxn.Fill = (SolidColorBrush)new BrushConverter().ConvertFromString("#841c34");   
                    }  
                }  
                public static async Task<bool> PingAsync(string host)  
                {  
                    try  
                    {  
                        var ping = new System.Net.NetworkInformation.Ping();  
                        var reply = await ping.SendPingAsync(host);  
          
                        return (reply.Status == System.Net.NetworkInformation.IPStatus.Success);  
                    }  
                    catch { return false; }  
                }  
                private async Task<bool> IsWebsiteUp_Ping(string url)  
                {  
                    Ping ping = new Ping();  
                    var hostName = new Uri(url).Host;  
          
                    PingReply result = await ping.SendPingAsync(hostName);  
                    return result.Status == IPStatus.Success;  
                }  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,686 Reputation points
    2022-11-24T16:51:15.017+00:00

    The point of DispatchTime is that it run on the UI thread. You should run the ping on a separate thread, and on completion dispatch to the ui thread to do the ui update.

    1 person found this answer helpful.
    0 comments No comments