title: Ping causes high cpu when disconnected from network
body: <p>I'm making a C# Pinger in WPF using Ping() and BackgroundWorker(). Here is my full code:</p>
  <pre><code>using System;
  using System.ComponentModel;
  using System.Net.NetworkInformation;
  using System.Runtime.InteropServices;
  using System.Threading;
  using System.Windows;
  using System.Windows.Media;
  
  namespace Pinger
  {
      public partial class MainWindow : Window
      {
          [DllImport(&quot;wininet.dll&quot;)] private static extern bool InternetGetConnectedState(out int Description, int ReservedValue);
  
          public static bool IsConnectedToInternet() { return InternetGetConnectedState(out _, 0); }
  
          private readonly BackgroundWorker BackWork_Pinger = new BackgroundWorker();
  
          private int counter = 0; private readonly Ping pinger = new Ping();
  
          public MainWindow()
          {
              InitializeComponent();
  
              BackWork_Pinger.WorkerReportsProgress = true;
  
              BackWork_Pinger.DoWork += (_, args) =&gt; { for (int i = 1; i &gt; 0; ++i) { if (IsConnectedToInternet()) { BackWork_Pinger.ReportProgress(i); Thread.Sleep(100); } } };
  
              BackWork_Pinger.ProgressChanged += (_, args) =&gt;
              {
                  try
                  {
                      PingReply pingReply = pinger.Send(&quot;8.8.8.8&quot;);
  
                      if (pingReply != null)
                      {
                          switch (pingReply.Status)
                          {
                              case IPStatus.Success:
  
                                  int PingNumber = Convert.ToInt32(pingReply.RoundtripTime);
  
                                  if (PingNumber &lt; 100) { Ping_Value.Foreground = Brushes.Lime; Ping_Value.Text = PingNumber + &quot; ms&quot;; }
  
                                  else if (PingNumber &lt; 300) { Ping_Value.Foreground = Brushes.Yellow; Ping_Value.Text = PingNumber + &quot; ms&quot;; }
  
                                  else if (PingNumber &lt; 500) { Ping_Value.Foreground = Brushes.DarkOrange; Ping_Value.Text = PingNumber + &quot; ms&quot;; }
  
                                  else { Ping_Value.Foreground = Brushes.Red; Ping_Value.Text = &quot;Bad&quot;; }
  
                                  break;
  
                              case IPStatus.TimedOut: Ping_Value.Foreground = Brushes.WhiteSmoke; Ping_Value.Text = &quot;Disconnected&quot;; break;
  
                              default: Ping_Value.Foreground = Brushes.WhiteSmoke; Ping_Value.Text = &quot;Disconnected&quot;; break;
                          }
                      }
                      else { Ping_Value.Foreground = Brushes.WhiteSmoke; Ping_Value.Text = &quot;Disconnected&quot;; }
                  }
                  catch { Ping_Value.Foreground = Brushes.WhiteSmoke; Ping_Value.Text = &quot;Disconnected&quot;; }
  
                  Counter.Content = counter.ToString(); counter++;
              };
  
              BackWork_Pinger.RunWorkerAsync();
          }
      }
  }
  </code></pre>
  <p>I wanna make a Pinger that works fine and not freezing my WinForm while disconnected or bad RoundTripTime. Is there any help to make a smooth pinger? or is there any demo project that i can modify from? Thank youuu :)</p>