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.
- Using Visual Studio 2005 create a new Windows form application. Within the main form place a label called “LblConnectStatus” as shown below.
- 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.
Comments
Anonymous
December 08, 2005
The comment has been removedAnonymous
December 09, 2005
I agree with Peter that network connection changes are difficult to detect and MS API's have problems.
Several years ago, I was writing a light-weight messaging system in C++ with the WinInet library. I needed to detect a change in network status for message rollback, etc. I decided to do the ultimate test and pulled my network cable. The code that I was stepping through to push data over the wire just kept merrily sending and receiving "acknowledgments" from the "receiving end". This scared the heck out of me, because I needed to depend upon an error condition to resend the data at a later time, and not flag it as successfully sent.
In subsequent testing, I was never able to reproduce this phenomenon. This application continues working to this day - and I'm not aware that this condition has ever occurred in production.
For sure, there are some bugs somewhere - which gives us developers a great sense of reliability... :o(Anonymous
December 09, 2005
I have similar concerns but I downloaded the sample application and ran it and found that it seemed to work well when unplugging the network connection (wired) or disabling the wireless.
This will give you connectivity. To check for endpoints there is also a new ping class that seems to work nicely as well.
Used in combination, it is serving me well.Anonymous
December 19, 2005
This is for Rob:
The problem is not, and has never been, "does it work for me, once, in my office". The problem is, "what percentage of end users will this fail for".
For example: one of the Microsoft APIs for network detection would fail if the end user started with a regular modem ISP, then upgraded to (IIRC) a cable modem, but didn't use the wizard to update. Your test will did not check this condition.
Another example: AOL 9.0 for Broadband will set you up so that you actually used ethernet, but as a RAS device, but not a modem-based RAS device. You can figure out that the ethernet is working, but there's no way to figure out that the ethernet is being used for connections. Again, your test does not check this condition.
Again: I don't care if it works for you, once. I care tremendously on knowing how many people it will fail for. If there isn't strong documentation and evidence that Microsoft has seriously tested this API in the real world then it will simply fall into my list of "yet another network API". Maybe I'll test it; maybe I won't. In all cases, I'm not going to use it until I can prove to my manager that it won't horribly break my application.Anonymous
December 21, 2005
So what testing are you looking for? Sounds like you have specific scenarious in mind. Is this part of an existing application?
The My namespace is really just a wrapper around various documented API's.
I don't really think you could fairly say what would fail for everybody given the varying software and hardware systems that exist. I would venture that this would be for any application or any piece of hardware.
Any application you architect and develop should always be tested fully for your hardware and software requirements. Part of the development process. If you have a requirement for a specific scenario then you should definately test that scenario.
Not sure how you could prove to your manager any application, enhancement or change works until you fully test any and all pieces.
Just my 2cAnonymous
January 21, 2009
PingBack from http://www.keyongtech.com/717863-my-computer-network-isavailable-questionAnonymous
June 15, 2009
PingBack from http://einternetmarketingtools.info/story.php?id=23482