Detecting Network Change Events

J

John Wright

I have been trying to get my program to detect the network status of my
program. On program startup I have the following code

If My.Computer.Network.IsAvailable = true then
'do some gui updating
else
'do other updating
end if

This is in the load function of my form. Right now if I run my program
without the cable attached (windows pops up the message Network cable is
unplugged and exchange also complains), it still says the network is
connected.

I have tried using the application events to check the status and it never
fires. So I used the following code:

Private NetAvailHandler As NetworkAvailabilityChangedEventHandler
Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
NetAvailHandler = New
NetworkAvailabilityChangedEventHandler(AddressOf
NetworkChange_NetworkAvailabilityChanged)
AddHandler NetworkChange.NetworkAvailabilityChanged, NetAvailHandler

End Sub

Private Sub NetworkChange_NetworkAvailabilityChanged(ByVal sender As
Object, ByVal e As NetworkAvailabilityEventArgs)
If Me.InvokeRequired Then
Invoke(NetAvailHandler, New Object() {sender, e})
Return
End If

End Sub
''' <summary>
''' updates the GUI according to network status
''' </summary>
''' <remarks></remarks>
Private Sub UpdateNetworkInfo()
If NetworkInterface.GetIsNetworkAvailable = False Then
tsNetworkStatus.Image = ImageList1.Images(1)
tsNetworkStatus.ToolTipText = "Network Offline"
Else
tsNetworkStatus.Image = ImageList1.Images(0)
tsNetworkStatus.ToolTipText = "Network Online"

End If
End Sub

And it too fails to fire this event. Am I missing something? Why can't my
..net programs check for these events? I have put breakpoints in these
locations to see if they are firing and erroring, but they don't fire.
Anyone have any suggestions?
 
S

Scott M.

Placing your code in the applicaiton events is the correct approach and the
following has always worked for me:

[ApplicationEvents.vb]

Namespace My

' The following events are available for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is
created.
' Shutdown: Raised after all application forms are closed. This event is
not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled
exception.
' StartupNextInstance: Raised when launching a single-instance application
and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is
connected or disconnected.
Partial Friend Class MyApplication

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

If My.Computer.Network.IsAvailable Then
MessageBox.Show("Network is now available.")
Else
MessageBox.Show("Network is now not available.")
End If

End Sub

End Class

End Namespace

You've got to make sure this code is in the MyApplication class, which can
be automatically created by selecting Application Events in your project's
property pages.

-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top