Handling the Server Lost Event

The last thing you will need to cover is ensuring that the client can react correctly if the server is lost. You've handled this in the peer-to-peer session already, and the code here is identical. First, you add a hook for the SessionTerminated event:

connection.SessionTerminated += new
    SessionTerminatedEventHandler(OnSessionTerminate);

Plus you'll need the actual event handler:

private void OnSessionTerminate(object sender, SessionTerminatedEventArgs e)
{
    this.BeginInvoke(new DisconnectCallback(OnDisconnect), null);
}
private void OnDisconnect()
{
    EnableSendDataButtons(false);
    AddText("Session terminated.");
    connected = false;

    // Dispose of our connection, and set it to null
    connection.Dispose();
    connection = null;
}

All you really want to do in this scenario is make sure you dispose the connection and nullify the object. However, it's good practice to inform the user by updating the UI and disabling the buttons.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.62.105