Поделиться через


NetworkChange.NetworkAddressChanged Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when the IP address of a network interface changes.

Namespace:  System.Net.NetworkInformation
Assembly:  System.Net (in System.Net.dll)

Syntax

'Declaration
Public Shared Event NetworkAddressChanged As NetworkAddressChangedEventHandler
public static event NetworkAddressChangedEventHandler NetworkAddressChanged

Remarks

The NetworkChange class raises NetworkAddressChanged events when the address of a network interface, also called a network card or adapter, changes.

To have a NetworkChange object call an event-handling method when a NetworkAddressChanged event occurs, you must associate the method with a NetworkAddressChangedEventHandler delegate, and add this delegate to this event.

Examples

The following example uses a NetworkAddressChangedEventHandler to handle NetworkAddressChanged events and detect when network availability changes.

Public class Example

    Dim Shared online As Boolean = False

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        ' Initialize the online variable and set a NetworkChange handler
        SetupNetworkChange()

        outputBlock.Text &= "Network is: "
        If online Then
            outputBlock.Text &= "online"
        Else   
            outputBlock.Text &= "offline"
        End If    

        ' Now start the main work of the application        
    End Sub

    ' Subroutine that gets called when the network changes 
    Private Shared Sub OnNetworkChange (ByVal sender As object, _
       ByVale AS EventArgs)

       If NetworkInterface.GetIsNetworkAvailable() Then
           If Not online Then
              online = True
              ' do what is needed to GoOnline()
           End If
       Else 
           If online Then
               online = False
               ' do what is needed to GoOffline()
           End If
       End If
    End Sub

    Private Shared Sub SetupNetworkChange()
        ' Get current network availalable and store the 
        ' initial value for online value
        If NetworkInterface.GetIsNetworkAvailable() Then
            online = true
            ' do what is needed to GoOnline()
        Else    
            online = false
            ' do what is needed to GoOffline()
        End If    

        ' Now add a network change event handler to indicate
        ' network availability 
        AddHandler NetworkChange.NetworkAddressChanged , _
            AddressOf OnNetworkChange
    End Sub

End Class  
public class Example
{
  static bool online = false; 

  public static void Demo(System.Windows.Controls.TextBlock outputBlock)
  {

      // Initialize the online variable and set a NetworkChange handler
      SetupNetworkChange();

      outputBlock.Text += "Network is: ";
      if (online)
          outputBlock.Text += "online";
      else     
          outputBlock.Text += "offline";

      // Now start the main work of the application        
  }

  // method that gets called when the network changes
  static void OnNetworkChange(object sender, EventArgs e)
  {
     if (NetworkInterface.GetIsNetworkAvailable())
     {
         if (!online)
          {
             online = true;
             // do what is needed to GoOnline();
          }
     }
     else {
         if (online)
         {
             online = false;
             // do what is needed to GoOffline();
         }
     }
  }

  private static void SetupNetworkChange()
  {
     // Get current network availalability and store the 
     // initial value of the online variable
     if (NetworkInterface.GetIsNetworkAvailable())
     {
         online = true;
         // do what is needed to GoOnline();
     }
     else
     {
         online = false;
         // do what is needed to GoOffline();
     }

      // Now add a network change event handler to indicate
      // network availability 
      NetworkChange.NetworkAddressChanged +=
          new NetworkAddressChangedEventHandler(OnNetworkChange);
  }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.