0

I am trying to scan the WiFi access points around me to scan their signal strengths. When I use ManagedNativeWifi Nuget (https://github.com/emoacht/ManagedNativeWifi) I can enumerate all Wifi access points:

            foreach (var network in NativeWifi.EnumerateBssNetworks())
            {
                this.textBox1.Text += $"{{Interface: {network.Interface.Description} ({network.Interface.Id})" + Environment.NewLine;
                this.textBox1.Text += $" SSID: {network.Ssid}" + Environment.NewLine;
                this.textBox1.Text += $" BSS: {network.BssType}" + Environment.NewLine;
                this.textBox1.Text += $" BSSID: {network.Bssid}" + Environment.NewLine;
                this.textBox1.Text += $" SignalStrength: {network.SignalStrength}" + Environment.NewLine;
                this.textBox1.Text += $" LinkQuality: {network.LinkQuality}" + Environment.NewLine;
                this.textBox1.Text += $" Frequency: {network.Frequency} KHz" + Environment.NewLine;
                this.textBox1.Text += $" Band: {network.Band} GHz" + Environment.NewLine;
                this.textBox1.Text += $" Channel: {network.Channel}}}" + Environment.NewLine;
            }

But when I try to run this code again, I only get the connected Wifi connection back and all others aren't displayed.

When I try to use this method my application just stops responding.

        public static Task RefreshAsync()
        {
            return NativeWifi.ScanNetworksAsync(timeout: TimeSpan.FromSeconds(10));
        }

By calling it from another method

            Task task_Refresh = RefreshAsync();

            task_Refresh.Wait();

Does anyone know what I am doing wrong? By the way whenever I click the Wifi symbol in the Windows Taskbar it searches all the Wifi networks and above code displays all again.

Sidenote I am using .net Framework 4.7 Windows Forms App.

Best regards and thank you for your time, Rémy Samulski

LiQuick.net
  • 189
  • 15
  • 2
    `task_Refresh.Wait();` is blocking. Make `RefreshAsync()` async and `await NativeWifi.ScanNetworksAsync(timeout: TimeSpan.FromSeconds(10));` – Jimi Jan 09 '22 at 15:26
  • 1
    Does this answer your question? [What's the difference between Task.Start/Wait and Async/Await?](https://stackoverflow.com/questions/9519414/whats-the-difference-between-task-start-wait-and-async-await) – Charlieface Jan 09 '22 at 15:54
  • Thank you Jimi and Charlieface. With the modifications Jimi suggested I can run it smoothly and I have tried to read the post Charlieface suggested but still are stuck in the 90's programming I guess and not fully understand the await and wait/result differences. I understand the posting but lack the ability to understand why the program just stops responding. – LiQuick.net Jan 09 '22 at 22:13
  • I came up wiith another solution while reading through stuff mentioned in the post Charlieface suggested: `Task> task_Refresh = NativeWifi.ScanNetworksAsync(timeout: TimeSpan.FromSeconds(10));` and `task_Refresh.ContinueWith(t => ShowRefresh(t.Result));` – LiQuick.net Jan 09 '22 at 22:57

0 Answers0