I am trying to create a background worker for getting the latest RSSI and link quality for all the available WLAN networks. However, if I initialise the WlanClient inside this background worker, I get this error message.
An attempt was made to establish a session to a network server, but there are already too many sessions established to that server vb.net
However, if I create a public class WlanClient, it does not update the RSSI and link quality values as the session is kept open and is not closed. Not sure how to close the session. However, here is the code that I use as the background worker.
Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
Try
'Dim wlan As New WlanClient()
'For Each wlanIface As WlanClient.WlanInterface In wlan.Interfaces
For Each wlanIface As WlanClient.WlanInterface In WiFi.client.Interfaces
Dim wlanBssEntries As Wlan.WlanBssEntry() = wlanIface.GetNetworkBssList()
For Each network As Wlan.WlanBssEntry In wlanBssEntries
Dim rss As Integer = network.rssi
Dim macAddr As Byte() = network.dot11Bssid
tMac = ""
For i As Integer = 0 To macAddr.Length - 1
If tMac = "" Then
tMac += macAddr(i).ToString("x2").PadLeft(2, "0"c).ToUpper()
Else
tMac += ":" & macAddr(i).ToString("x2").PadLeft(2, "0"c).ToUpper()
End If
Next
Dim ssid As String = Encoding.ASCII.GetString(network.dot11Ssid.SSID, 0, CInt(network.dot11Ssid.SSIDLength))
Dim available As Integer = 0
Dim rowindex As Integer = -1
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value.ToString().Equals(ssid) AndAlso row.Cells(1).Value.ToString().Equals(tMac) Then
available = 1
rowindex = row.Index
Exit For
End If
Next
If available = 0 Then
If Me.IsDisposed = True Then
Exit Sub
End If
DataGridView1.Rows.Add(ssid, tMac, network.dot11BssPhyType, rss, network.linkQuality)
Else
If DataGridView1.Rows(rowindex).Cells(3).Value <> rss Or DataGridView1.Rows(rowindex).Cells(4).Value <> network.linkQuality Then
DataGridView1.Rows(rowindex).Cells(3).Value = rss
DataGridView1.Rows(rowindex).Cells(4).Value = network.linkQuality
End If
End If
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
Application.DoEvents()
Thread.Sleep(100)
End Sub
Public Class WiFi
Public Shared client As New WlanClient()
End Class
Kindly let me know on how to close the client whenever required or dispose a locally declared WlanClient.
Found an answer via How do i reset the system cache of WLAN info? Adding
wlanIface.Scan()
after the firstFor
loop solved the issue. Now the scan is picking up new data.