You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ManagedNativeWifi无法刷新无线网络枚举问题求助

Fixing ManagedNativeWifi's Repeated WiFi Enumeration & Unresponsive Scan Issues

Hey Rémy, let's tackle the two problems you're facing with ManagedNativeWifi in your .NET Framework 4.7 WinForms app:

  1. Why your second scan only shows connected WiFi networks instead of all nearby access points
  2. Why calling ScanNetworksAsync with Wait() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 08:37:44