我使用VB.NET
开发了winform应用程序。该应用程序部署在连接到无线网络的机器中。机器在车内(移动物体)。
应用程序已DataGridView
加载了来自MSSQL Server(在远程计算机中)的数据。数据每5秒刷新一次。
我已使用NetworkAvailabilityChanged
事件来检测网络状态。如果网络可用,那么我从表中检索数据。
代码:
AddHandler NetworkChange.NetworkAvailabilityChanged, AddressOf NetworkStateChangeHandler
Public Sub NetworkStateChangeHandler(ByVal sender As Object,
ByVal e As NetworkAvailabilityEventArgs)
If e.IsAvailable = True Then
g_bNetworkAlive = True
Else
g_bNetworkAlive = False
End If
End Sub
private Sub GetData()
If g_bNetworkAlive = True
'code to get the data from table
End If
End Sub
问题:
如果汽车movers out of the out of the network
,NetworkAvailabilityChanged
事件未被触发。因此它每5秒抛出以下错误,应用程序崩溃。
A network-related or instance-specific error occurred while establishing
a connection to SQL Server. The server was not found or was not accessible.
临时修复:我每隔5秒向SQL Server计算机发出Ping
请求,以检测网络状态。它会影响应用程序的性能。
注意:如果我手动关闭Wifi,请NetworkAvailabilityChanged event is fired
。问题出在汽车驶出网络时。
是否还有其他可行的解决方案来检测无线网络状态?
答案 0 :(得分:2)
为什么当你可以检查你是否连接到本地wifi时,为某些虚拟网站发送http请求。 使用ManagedWifi API,这将消除您面临的减速。
//Create object at the beginning of your application
WlanClient wlanClient = new WlanClient();
//You this code to check wifi availability wherever you need
foreach (WlanInterface _interface in wlanClient.Interfaces)
{
If (_interface.CurrentConnection.wlanAssociationAttributes.dot11Ssid.SSID!=null)
{
// You are connected to wifi
}
}
编辑:如果您没有找到dll,direct link。只需参考Dll和Voila!你完成了。
答案 1 :(得分:1)
将您的代码更改为
(Get-Content $path)
答案 2 :(得分:1)
您也可以使用WebRequest检查您是否有互联网连接:
Private Sub GetData()
If HasInternet AndAlso g_bNetworkAlive Then
'code to get the data from table
End If
End Sub
Public Shared Function HasInternet As Boolean
Return Not (HttpGet("http://www.google.com/") = "Error")
End Function
Public Shared Function HttpGet(url As String) As String
Dim request As WebRequest = WebRequest.Create(url)
request.Method = "GET"
Try
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
dataStream.Close()
response.Close()
Return responseFromServer
Catch ex As Exception
Return "Error"
End Try
End Function
您可以使用以下函数检查sql连接:
Private Function IsDatabaseConnected() As Boolean
Try
Using sqlConn As New SqlConnection("YourConnectionString")
sqlConn.Open()
Return (sqlConn.State = ConnectionState.Open)
End Using
Catch e1 As SqlException
Return False
Catch e2 As Exception
Return False
End Try
End Function
答案 3 :(得分:1)
我只想使用try..catch
,所以如果你得到异常,你可以根据它的id知道代码失败的原因,然后决定下一步做什么,最后你可以执行更多的代码而不管正在检索的数据
Private Sub GetData()
Try
'code to get the data from table
Catch ex As Exception
' Show the exception's message.
MessageBox.Show(ex.Message)
' Show the stack trace, which is a list of methods
' that are currently executing.
MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
Finally
' This line executes whether or not the exception occurs.
MessageBox.Show("in Finally block")
End Try
End Sub