ManagedNativeWifi无法刷新无线网络枚举问题求助
Hey Rémy, let's tackle the two problems you're facing with ManagedNativeWifi in your .NET Framework 4.7 WinForms app:
- Why your second scan only shows connected WiFi networks instead of all nearby access points
- Why calling
ScanNetworksAsyncwithWait()makes your app freeze
First: The Unresponsive Scan Problem
When you use task_Refresh.Wait(), you're blocking the UI thread completely. ManagedNativeWifi's scan relies on interacting with Windows' network services, and if the UI thread is stuck waiting, the system can't finish the scan process—hence the app freeze.
The fix here is to use proper async/await instead of blocking. Async/await lets the UI stay responsive while the scan runs in the background.
Second: Fixing the Scan & Enumeration Flow
Rewrite your scan logic to use async/await in your UI event handler (like a button click):
// Mark your button click event as async private async void btnScanWifi_Click(object sender, EventArgs e) { // Clear the text box first to avoid duplicate entries textBox1.Clear(); // Optional: Show a loading message to improve user experience lblScanStatus.Text = "Scanning for WiFi networks..."; // Run the scan asynchronously—no UI blocking! await NativeWifi.ScanNetworksAsync(timeout: TimeSpan.FromSeconds(10)); // Enumerate networks once the scan is complete var networkBuilder = new StringBuilder(); foreach (var network in NativeWifi.EnumerateBssNetworks()) { networkBuilder.AppendLine($"Interface: {network.Interface.Description} ({network.Interface.Id})"); networkBuilder.AppendLine($" SSID: {network.Ssid}"); networkBuilder.AppendLine($" BSS: {network.BssType}"); networkBuilder.AppendLine($" BSSID: {network.Bssid}"); networkBuilder.AppendLine($" SignalStrength: {network.SignalStrength}"); networkBuilder.AppendLine($" LinkQuality: {network.LinkQuality}"); networkBuilder.AppendLine($" Frequency: {network.Frequency} KHz"); networkBuilder.AppendLine($" Band: {network.Band} GHz"); networkBuilder.AppendLine($" Channel: {network.Channel}"); networkBuilder.AppendLine(); // Add a blank line between networks } textBox1.Text = networkBuilder.ToString(); lblScanStatus.Text = "Scan complete!"; }
Why Clicking the System WiFi Icon Fixes It?
When you click the Windows taskbar WiFi icon, Windows triggers a full system-wide WiFi scan that updates its internal cache of nearby networks. Your original broken scan wasn't properly triggering this refresh (because the UI thread was blocked), so EnumerateBssNetworks() was only pulling stale cached data (just the connected network). The system's manual scan fixes that cache, which is why your enumeration works afterward.
Bonus Optimization
I swapped the direct textBox1.Text += with a StringBuilder—this is way more efficient, especially if there are lots of WiFi networks. Direct string concatenation creates tons of unnecessary string objects, which can slow down your UI.
内容的提问来源于stack exchange,提问作者LiQuick.net

