vb.net - 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.
解决方案
推荐阅读
- apache-kafka - Kafka Topic、Broker、ZooKeeper 架构概述
- json - Dart - 带有正文的 Http 获取请求
- r - R中的矢量化循环,将矩阵中每一行中的所有值相加
- python-3.x - (unicode 错误)“unicodeescape”编解码器无法解码位置 0-1 中的字节:截断 \uXXXX 转义
- javascript - Angular Firebase Cloud 返回不正确的数据
- sql-server - 更新未添加正确的值
- for-loop - 中断 for 循环并在发生特定条件时抛出异常
- google-cloud-platform - 如何通过 Cloud Build 访问 GSM Secret 并传递给 Cloud Function
- javascript - 如何使用父对象中的 ID 引用将主对象与子对象合并到每个子对象
- windows - MidiInProc 回调在哪个上下文中执行?