views:

97

answers:

1

I am building a simple application that listens to NetworkAvailibilityChanged Event. It's all good, except in the case of a modem PPP interface connection, where the app gets notified of the availibility only when the network is connected but not when disconnected.

Am I missing anything? I tried it for LAN and it worked perfectly.

+3  A: 

Here is what the NetworkAvailbilityChanged touches.

Friend Shared Function InternalGetIsNetworkAvailable() As Boolean
    If ComNetOS.IsWinNt Then
        Dim interface2 As NetworkInterface
        For Each interface2 In SystemNetworkInterface.GetNetworkInterfaces
            If (((interface2.OperationalStatus = OperationalStatus.Up) AndAlso (interface2.NetworkInterfaceType <> NetworkInterfaceType.Tunnel)) AndAlso (interface2.NetworkInterfaceType <> NetworkInterfaceType.Loopback)) Then
                Return True
            End If
        Next
        Return False
    End If
    Dim flags As UInt32 = 0
    Return UnsafeWinINetNativeMethods.InternetGetConnectedState((flags), 0)
End Function

Private Shared Sub ChangedAddress(ByVal sender As Object, ByVal eventArgs As EventArgs)
    SyncLock AvailabilityChangeListener.syncObject
        Dim isNetworkAvailable As Boolean = SystemNetworkInterface.InternalGetIsNetworkAvailable
        If (isNetworkAvailable <> AvailabilityChangeListener.isAvailable) Then
            AvailabilityChangeListener.isAvailable = isNetworkAvailable
            Dim array As DictionaryEntry() = New DictionaryEntry(AvailabilityChangeListener.s_availabilityCallerArray.Count  - 1) {}
            AvailabilityChangeListener.s_availabilityCallerArray.CopyTo(array, 0)
            Dim i As Integer
            For i = 0 To array.Length - 1
                Dim key As NetworkAvailabilityChangedEventHandler = DirectCast(array(i).Key, NetworkAvailabilityChangedEventHandler)
                Dim context As ExecutionContext = DirectCast(array(i).Value, ExecutionContext)
                If (context Is Nothing) Then
                    key.Invoke(Nothing, New NetworkAvailabilityEventArgs(AvailabilityChangeListener.isAvailable))
                Else
                    ExecutionContext.Run(context.CreateCopy, AvailabilityChangeListener.s_RunHandlerCallback, key)
                End If
            Next i
        End If
    End SyncLock
End Sub

According to these it seems that if its telling you there is network, it should also tell you there is not network. It also appears to work off of the Address for each device. When the Address changes, it calls ChangedAddress.

Take a look at these and see if you get a clearer picture of what is going on behind the scenes.

Jimmie Clark
Thanx for the post, the code is good and clear but as i see, it requires some native calls and there is no problem with that, because i have solved the problem in a similar fashion by using interop with the SENS architecture and the COM+ event system, the weird thing is that the .NET function i talked about after some testing, seemed to work with the modem only if you're connected at the moment the application is running, then it detects the disconnection, but if u connect again and disconn. it won't .. !!! but works fine with LAN with some latency, anyway thanks again, AB
Ebraheem Najjar