The Occasionally Connected Application

The occasionally connected application is becoming an important part of many enterprises. The goal of these types of applications is to provide a consistent and reliable experience for users regardless of their current network status. Of course for developers this means monitoring the current network connection status and enabling their application to respond intuitively to any changes. The combination of Visual Studio 2005 and the “My” object provides an event based model to enable this within your application.

As an example, follow these steps to build a simple network aware application (or download it from here) that notifies the user when their current connectivity changes.

  1. Using Visual Studio 2005 create a new Windows form application. Within the main form place a label called “LblConnectStatus” as shown below.

  1. When the application is initially loaded you will first want to check the current network connection state and update the label. This can be done from the form_load event using the “My” class and the following code.

        If My.Computer.Network.IsAvailable = True Then

            LblConnectStatus.Text = "Connected"

        Else

            LblConnectStatus.Text = "Not Connected"

        End If

At this point when the application starts it will capture the initial network status. The real problem is that this doesn’t track any network changes that may occur during the running of your application. For example, the wireless network goes down. To solve this Visual Studio 2005 introduces a set of application level events available within the “My projects tab as shown below.

One of these events is the MyApplication_NetworkAvailabilityChanged event. This event is designed to provide access to the current connection state of the application. Essentially, allowing the application to automatically raise the NetworkAvailabilityChanged event every time the availability of the current network connection changes. Within this event the IsNetworkAvailable property of the ‘e’ parameter can be used to get the new state of the network connection. By default, this event is raised on the application’s main thread with the other user interface events. This allows the event handler to directly access the applications UI.

Within this event enter the following code to respond to this event.

Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged

            If e.IsNetworkAvailable = True Then

                My.Forms.MainForm.LblConnectStatus.Text = "Connected"

            Else

    My.Forms.MainForm.LblConnectStatus.Text = "Not  

    Connected"

            End If

        End Sub

Once you have entered the code, start up the application. Personally, to test it, I turned on and off my wireless connection a few times. If you either connect or disconnect from the network you can see that the label changes to reflect your current state.

For further reference you can download the sample application here.