Monday, May 21, 2007

Handling Connectivity Change Events

detect and handle changes to networks and connectivity

The Network and the Connection classes both raise a StateChanged event when their state changes. Create a handler for this event that accepts an instance of the StateChangedEventArgs class, which exposes the IsConnected property

public void StateChangeHandler(object sender, StateChangedEventArgs e)
{
if (e.IsConnected)
{
MessageBox.Show("Now connected");
}
else
{
MessageBox.Show("Now disconnected");
}
}

Connect your event handler to the StateChanged event of the Connection or Network you want to monitor.
conn.StateChanged += StateChangeHandler;
netwk.StateChanged += StateChangeHandler;
The Connection class exposes a method named UpdateStatus that you can call to raise the StateChanged event and obtain information through your event handler on the connectivity status.
conn.UpdateStatus();

No comments: