title: Ping Completed Event Handler not working
body: <p>When I execute this code in command line, it's working fine:</p>
  
  <pre><code>class Program
  {
  
      private static List&lt;Ping&gt; pingers = new List&lt;Ping&gt;();
      private static List&lt;string&gt; value = new List&lt;string&gt;();
      private static int instances = 0;
      private static object @lock = new object();
      private static int result = 0;
      private static int timeOut = 2500;
      private static int ttl = 7;
      public static void Main()
      {
          string baseIP = "192.168.1.";
  
          Console.WriteLine("Pinging  destinations of D-class in {0}*", baseIP);
  
          CreatePingers(254);
  
  
          PingOptions po = new PingOptions(ttl, true);
          System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
          byte[] data = enc.GetBytes("");
  
          SpinWait wait = new SpinWait();
          int cnt =1;
  
          Stopwatch watch = Stopwatch.StartNew();
  
          foreach (Ping p in pingers)
          {
              lock (@lock)
              {
                  instances += 1;
              }
  
              p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
              cnt += 1;
          }
  
          //while (instances &gt; 0)
          //{
          //    wait.SpinOnce();
          //}
  
          watch.Stop();
          for (int i = 0; i &lt; value.Count; i++)
          {
              Console.WriteLine(value[i]);
          }
          DestroyPingers();
  
          Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
          Console.ReadKey();
  
      }
  
      public static void Ping_completed(object s, PingCompletedEventArgs e)
      {
          lock (@lock)
          {
              instances -= 1;
          }
  
          if (e.Reply.Status == IPStatus.Success)
          {
  
             string sa = string.Concat("Active IP: ", e.Reply.Address.ToString());
             value.Add(sa);
            //Console.WriteLine(sa);
              String diachiip = e.Reply.Address.ToString();
              result += 1;
  
          }
          else
          {
              //Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
          }
      }
  
  
      private static void CreatePingers(int cnt)
      {
          for (int i = 1; i &lt;= cnt; i++)
          {
              Ping p = new Ping();
              p.PingCompleted += Ping_completed;
              pingers.Add(p);
          }
      }
  
      private static void DestroyPingers()
      {
          foreach (Ping p in pingers)
          {
              p.PingCompleted -= Ping_completed;
              p.Dispose();
          }
  
          pingers.Clear();
  
      }
  }
  </code></pre>
  
  <p>But when I convert from it to window form, it doesn't work. I don't kwow why, I have tried many different ways...</p>
  
  <p>Code is here:</p>
  
  <pre><code>public partial class Form1 : Form
  {
  
      public static List&lt;Ping&gt; pingers = new List&lt;Ping&gt;();
      public static List&lt;string&gt; value = new List&lt;string&gt;();
      public static int instances = 0;
      public static object @lock = new object();
      public static int result = 0;
      public int timeout = 2500;
      public static int ttl = 7;
      public Form1()
      {
          InitializeComponent();
      }
  
      public void btnscan_Click(object sender, EventArgs e)
      {
          string baseIP = "192.168.1.";
          //int kt = Int32.Parse(txtkt.Text);
          //int start = Int32.Parse(txtstart.Text);
          CreatePingers(254);
          PingOptions po = new PingOptions(ttl, true);
          System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
          byte[] data = enc.GetBytes("");
          int cnt = 1;
          Stopwatch watch = Stopwatch.StartNew();
          foreach (Ping p in pingers)
          {
              lock (@lock)
              {
                  instances += 1;
  
              }
              p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeout, data, po);
              cnt += 1;        
          }
          watch.Stop();
          //Result alway return 0
          lst1.Items.Add(result.ToString());
          lst1.Items.Add(value.Count.ToString());
          for (int i = 0; i &lt; value.Count; i++)
          {
              lst1.Items.Add(value[i]);
              lst1.Items.Add("\n");
          }
          DestroyPingers();
          string a = "Finished in " + watch.Elapsed.ToString() + ". Found " + result + " active IP-addresses.";
          lst1.Items.Add(a);
      }
  
  
      public static void CreatePingers(int kt)
      {
          for (int start = 1; start &lt;= kt; start++)
          {
              // class System.Net.NetworkInformation.Ping
              Ping p = new Ping();
              p.PingCompleted += Ping_completed();
              pingers.Add(p);
          }
      }
  
       public static PingCompletedEventHandler Ping_completed()
      {
           PingCompletedEventHandler a = new PingCompletedEventHandler(abc);
           return a;
      }
  
      static void abc(object s, PingCompletedEventArgs e)
       {
           value.Add("abc");
           lock (@lock)
           {
               instances -= 1;
           }
           if (e.Reply.Status == IPStatus.Success)
           {
               string abcd = string.Concat("Active IP: ", e.Reply.Address.ToString());
               value.Add(abcd);
               result += 1;
           }
       }
       public static void DestroyPingers()
       {
           foreach (Ping p in pingers)
           {
               p.PingCompleted -= Ping_completed();
               p.Dispose();
           }
           pingers.Clear();
       }
  }
  </code></pre>
  
  <p>What is wrong in this code?</p>