title: Why won't NetworkChange events fire
body: <p>I am fairly new to all this but I have looked through questions and can't find anything that answers my question.</p>
  
  <p>I am writing a simple service that checks whether it can reach one of our servers whenever there is a change to it's network settings.</p>
  
  <p>I am using the two events under the NetworkChange class, NetworkAddressChanged and NetworkAvailabiltyChanged.</p>
  
  <p>When either fire the service tries to ping the server and depending on the result changes the ProxyEnable setting.</p>
  
  <p>The Code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Diagnostics;
  using System.Linq;
  using System.ServiceProcess;
  using System.Text;
  using System.Net.NetworkInformation;
  using Microsoft.Win32;
  
  
  namespace ProxyManager
  {
      public partial class ProxyManager : ServiceBase
      {
          static RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings",true);
          static string currentProxy = rk.GetValue("ProxyEnable").ToString();
          static string newProxy;
  
          public ProxyManager()
          {
              InitializeComponent();
  
              NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
              NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
              newProxy = "0";
          }
  
          protected override void OnStart(string[] args)
          {
  
          }
  
          void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
          {
              ProxySwitcher();
              EventLog evt = new EventLog("ProxyManager");
              string message = "Proxy Manager, Newtwork availabilty changed.  Proxy switched to " + newProxy.ToString();
              evt.Source = "Proxy Manager";
              evt.WriteEntry(message,EventLogEntryType.Information);
  
          }
  
          void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
          {
              ProxySwitcher();
              EventLog evt = new EventLog("ProxyManager");
              string message = "Proxy Manager, Newtwork address changed.  Proxy switched to " + newProxy.ToString();
              evt.Source = "Proxy Manager";
              evt.WriteEntry(message,EventLogEntryType.Information);
          }
  
          void ProxySwitcher()
          {    
              if (currentProxy == "0")
              {
                  newProxy = "1";
              }
              else
              {
                  newProxy = "0";
              }
  
              try
              {
                  Ping myPing = new Ping();
                  string host = "FILE SERVER";
                  byte[] buffer = new byte[32];
                  int timeout = 1000;
                  PingOptions pingOptions = new PingOptions();
                  PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
                  if (reply.Status == IPStatus.Success)
                  {
                      if (currentProxy == "0")
                      {
  
                          rk.SetValue("ProxyEnable", newProxy);
                      }
                  }
                  else
                  {
                      if (currentProxy == "1")
                      {
                          rk.SetValue("ProxyEnable", newProxy);
                      }
                  }
              }
              catch (PingException pingEx)
              {
                  rk.SetValue("ProxyEnable", 0);
              }
          }
          protected override void OnStop()
          {
          }
      }
  }
  </code></pre>
  
  <p>My problem is that when the service is installed and running, the events are either not being triggered or not being picked up.  There related events are not being logged and the proxy is not switching.</p>
  
  <p>I tried the code, minus the event handlers in a console app that I could run at will and that worked fine, depending on the network availability.</p>
  
  <p>I am running Windows 7 x86, but am coding in .NET 3.5.</p>
  
  <p>Any help will be gratefully received.</p>