首页 > 解决方案 > How to check Network Availability

问题描述

I want to make dll to check network availability. If Network not available I display message & if the message reply=Retry its check again after 5 seconds. With in the 5 seconds if the network established again exit from the dll & open the main form.

My problem is I cannot display message if the use press cancel to the retry message.

This is my sample code

Main Form

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            
            clsConnectionAvailability.CheckNetworkConnectivity()

                        txtUserName.Text = Environment.UserName
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

This is my dll

    Imports System.Windows.Forms
Imports System.Net.NetworkInformation

Public Class clsConnectionAvailability

    Private Shared WithEvents WaitTimer As New Timer()
    Private Shared intAlarmCounter As Integer = 1

    Public Shared Sub CheckNetworkConnectivity()
        ' Checking Network Connectivity
        If Not NetworkInterface.GetIsNetworkAvailable Then ' If Netowrk Connection Unavailable
            clsIsNetworkConnect.CheckNetWorkConnection() ' Checking Network Connection Is Available or Retry Until Available
        End If
    End Sub

End Class


    Imports System.Windows.Forms

Public Class clsIsNetworkConnect

    ' Timer Inizialization Variables Declaration
    Private Shared WithEvents WaitTimer As New Timer()
    Private Shared intAlarmCounter As Integer = 1
    Private Shared booExitFlag As Boolean = False

    Public Shared Sub CheckNetWorkConnection()
        ' Time Event Inizialization
        With WaitTimer
            .Interval = 5000 ' TimerEventProcessor will Fire after { 5 Seconds }
            .Start()
        End With

        While booExitFlag = False
            ' After Interval fulfilled, TimerEventProcessor Fired
            Application.DoEvents()
        End While

    End Sub

    Private Shared Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs) Handles WaitTimer.Tick
        ' What to Do When Timer Interval Fulfilled ?
        WaitTimer.Stop()
        Dim msgConectResult As DialogResult = MessageBox.Show("Network Connectivity Unavailable." & vbCr & _
                                                              "Please Inform IT Department." & vbCr & _
                                                              "Retry - Check For Network Connectivity Availability Again." & vbCr & _
                                                              "Cancel - Exit From The System." & vbCr & _
                                                              "Network Unavailable", _
                                                              MessageBoxButtons.RetryCancel, _
                                                              MessageBoxIcon.Information, _
                                                              MessageBoxDefaultButton.Button1) ' Display Message & Waiting for Reply
        If msgConectResult = DialogResult.Retry Then ' DialogResult = Retry
            If Not System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable Then ' If Netowrk Connection Unavailable
                intAlarmCounter += 1
                WaitTimer.Enabled = True
            Else ' If Network Connection Available
                booExitFlag = True
            End If
        Else ' DialogResult = Cancel
            booExitFlag = True
            Application.Exit()
        End If
    End Sub
End Class

can anyone please help me to send cancel message or booExitFlag value to main form.

标签: vb.netdll

解决方案


推荐阅读