|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article describes an easy approach to building two controls used to monitor the status of an internet connection and provide the user with some indication of that status. Within the attached project, there are two controls, one shows the user what the connection type is and whether or not the machine is connected or offline, the other one is used to show some indication of the quality of the connection in terms of whether or not the connection is good, intermittent, or offline. The purpose of the controls is to provide a mobile user of a smart client application some status information regarding the internet connection; in this instance, there are many forms within the application, and I wanted a simple control to drop on each form to provide that status.
Figure 1: Both Connection Status Controls In Use Getting StartedIn order to get started, unzip the attachment, and load the solution into Visual Studio 2005. Examine the Solution Explorer, and note the files contained in each of the two projects:
Figure 2: The Solution Explorer Showing the Project Files The “ConnectStatus” project contains the two controls used to display the internet connection status to the user; the “ The second project, “TestAppForInetConnect”, contains a simple form used to display both controls at the same time. This second project is not necessary as with Visual Studio 2005, the user may display the controls in the control test container. There is no code associated with this second project, the main form has one of each type of control loaded into it, and it serves only as a container for those controls. The Code: ConnectStateViewThe control is quite simple; the visual elements include only a group box and a single label. Aside from the visual elements, there is a single timer which is used to check the status of the internet connection repeatedly, and there is a single image list which is used to hold a couple of images used to place an icon adjacent to the label control. If you care to open the class, you will note that there are no imports. The control’s code is pretty easy to read, and is as follows: Public Class ConnectStateView
#Region "Declarations"
Private ConnectionStateString As String
Private Declare Function InternetGetConnectedState Lib _
"wininet.dll" (ByRef lpSFlags As Int32, _
ByVal dwReserved As Int32) As Boolean
Public Enum InetConnState
modem = &H1
lan = &H2
proxy = &H4
ras = &H10
offline = &H20
configured = &H40
End Enum
#End Region
#Region "Control Methods"
Private Sub ConnectStateView_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
Dim blnState As Boolean
blnState = CheckInetConnection()
lblConnectStatus.Text = " Connection Type: " & _
ConnectionStateString
End Sub
Function CheckInetConnection() As Boolean
Dim lngFlags As Long
If InternetGetConnectedState(lngFlags, 0) Then
' True
If lngFlags And InetConnState.lan Then
ConnectionStateString = "LAN."
lblConnectStatus.Image = ImageList1.Images(1)
ElseIf lngFlags And InetConnState.modem Then
ConnectionStateString = "Modem."
lblConnectStatus.Image = ImageList1.Images(1)
ElseIf lngFlags And InetConnState.configured Then
ConnectionStateString = "Configured."
lblConnectStatus.Image = ImageList1.Images(1)
ElseIf lngFlags And InetConnState.proxy Then
ConnectionStateString = "Proxy"
lblConnectStatus.Image = ImageList1.Images(1)
ElseIf lngFlags And InetConnState.ras Then
ConnectionStateString = "RAS."
lblConnectStatus.Image = ImageList1.Images(1)
ElseIf lngFlags And InetConnState.offline Then
ConnectionStateString = "Offline."
Me.lblConnectStatus.Image = ImageList1.Images(2)
End If
Else
' False
ConnectionStateString = "Not Connected."
lblConnectStatus.Image = ImageList1.Images(3)
End If
End Function
#End Region
End Class
In the beginning of the code, note that there is a region called “Declarations” defined, and within that region, there are three declarations. The first declaration defines a string value that is used to keep track of the connection type used by the client’s machine. The next declaration is the most important part of the code, it is the code that exposes a function from the wininet.dll to the application; that function is called “ The last declaration is of an enumeration used to contain representatives of each connection type exposed by the previous function. The flags identified in the enumeration are representative of each connection type that may be returned from making a call to the wininet.dll function declared previously. After the declarations region is closed, a new region entitled “Control Methods” is defined. The first item in this section is the control load event handler; in this section, the timer is enabled, and the process of polling the internet connection of the timer’s interval is initialized. After the load event code, the handler for the timer is defined. The handler is used to evoke the “ The “
Figure 3: Connection Status Controls with Failed Internet Connection That is all there is to the code for this first control. The next section will discuss the content of the second control as used to gauge the quality of an internet connection. The Code: ConnectQualityViewThis control is used to give a rough estimate of the quality of the internet connection as a function of the durability and persistence of the connection over a brief time span (3 seconds). The code contained within this class is nearly identical to the previous class; it is as follows: Public Class ConnectQualityView
#Region "Declarations"
Dim ConnectionQualityString As String = "Off"
Private Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef lpSFlags As Int32, _
ByVal dwReserved As Int32) As Boolean
Public Enum InetConnState
modem = &H1
lan = &H2
proxy = &H4
ras = &H10
offline = &H20
configured = &H40
End Enum
#End Region
#Region "Control Methods"
Private Sub ConnectQualityView_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Timer1.Enabled = True
Me.DoubleBuffered = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
lblConnectStatus.Refresh()
Dim blnState As Boolean
blnState = CheckInetConnection()
End Sub
Function CheckInetConnection() As Boolean
Dim lngFlags As Long
If InternetGetConnectedState(lngFlags, 0) Then
' True
If lngFlags And InetConnState.lan Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
Me.Refresh()
ElseIf lngFlags And InetConnState.modem Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
ElseIf lngFlags And InetConnState.configured Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
ElseIf lngFlags And InetConnState.proxy Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
ElseIf lngFlags And InetConnState.ras Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
ElseIf lngFlags And InetConnState.offline Then
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Green
lblConnectStatus.Text = "Connection Quality: Good"
ConnectionQualityString = "Good"
Case "Off"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = _
"Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
End Select
End If
Else
' False
Select Case ConnectionQualityString
Case "Good"
lblConnectStatus.ForeColor = Color.DarkOrange
lblConnectStatus.Text = "Connection Quality: Intermittent"
ConnectionQualityString = "Intermittent"
Case "Intermittent"
lblConnectStatus.ForeColor = Color.Red
lblConnectStatus.Text = "Connection Quality: Off"
ConnectionQualityString = "Off"
Case "Off"
lblConnectStatus.ForeColor = Color.Red
lblConnectStatus.Text = "Connection Quality: Off"
ConnectionQualityString = "Off"
End Select
End If
End Function
#End Region
End Class
In reviewing this code in contrast to the previous class described, you will see the only real difference is that the status is used to set the text and fore color of the label control used to display the status, and you will notice that the previous status message (gathered 1.5 seconds earlier) is evaluated to determine how to display the status of the connection. This operates under a simple notion of promoting the status of the connection for remaining active over time. The control initializes with the connection quality string value set to “Off”; when the connection state is evaluated, the initial connection state is noted, and the code promotes the status to “Intermittent”. If the status remains active for an additional 1.5 seconds, it is promoted to “Good”. The idea here is that if the connection is dropping off and getting picked back up, the code will continually evaluate the current status against the previous status and promote or demote the status between the three available options of Good, Intermittent, and Off. Naturally, you may alter the amount of time between status checks, and in so doing, increase or decrease the amount of time necessary to promote or demote the status of the connection. That is all there is to the second control. SummaryWhilst this example project demonstrates a couple of ways in which you can monitor and display status regarding a machine’s internet connection, these approaches do not represent all of the ways that you may accomplish this task. Still and all, this approach is a simple and easy way to display connection status information to your users.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||