diff --git a/Source/NETworkManager/ViewModels/NetworkInterfaceViewModel.cs b/Source/NETworkManager/ViewModels/NetworkInterfaceViewModel.cs index 30e597ee57..6501f6d01c 100644 --- a/Source/NETworkManager/ViewModels/NetworkInterfaceViewModel.cs +++ b/Source/NETworkManager/ViewModels/NetworkInterfaceViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.Linq; @@ -32,8 +33,9 @@ namespace NETworkManager.ViewModels; public class NetworkInterfaceViewModel : ViewModelBase, IProfileManager { #region Variables + private static readonly ILog Log = LogManager.GetLogger(typeof(NetworkInterfaceViewModel)); - + private readonly IDialogCoordinator _dialogCoordinator; private readonly DispatcherTimer _searchDispatcherTimer = new(); private BandwidthMeter _bandwidthMeter; @@ -118,9 +120,9 @@ private set #region NetworkInterfaces, SelectedNetworkInterface - private List _networkInterfaces; + private ObservableCollection _networkInterfaces = []; - public List NetworkInterfaces + public ObservableCollection NetworkInterfaces { get => _networkInterfaces; private set @@ -629,7 +631,8 @@ private async Task LoadNetworkInterfaces() { IsNetworkInterfaceLoading = true; - NetworkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync(); + // Get all network interfaces... + (await NetworkInterface.GetNetworkInterfacesAsync()).ForEach(NetworkInterfaces.Add); // Get the last selected interface, if it is still available on this machine... if (NetworkInterfaces.Count > 0) @@ -695,7 +698,7 @@ private async Task ExportAction() catch (Exception ex) { Log.Error("Error while exporting data as " + instance.FileType, ex); - + var settings = AppearanceManager.MetroDialog; settings.AffirmativeButtonText = Strings.OK; @@ -935,22 +938,36 @@ private async Task RemoveIPv4AddressAction() private async void ReloadNetworkInterfaces() { + // Avoid multiple reloads + if(IsNetworkInterfaceLoading) + return; + IsNetworkInterfaceLoading = true; // Make the user happy, let him see a reload animation (and he cannot spam the reload command) await Task.Delay(2000); - var id = string.Empty; - - if (SelectedNetworkInterface != null) - id = SelectedNetworkInterface.Id; - - // Load network interfaces... - var networkItems = await Models.Network.NetworkInterface.GetNetworkInterfacesAsync(); - - // Change interface... - SelectedNetworkInterface = string.IsNullOrEmpty(id) ? networkItems.FirstOrDefault() : networkItems.FirstOrDefault(x => x.Id == id); - NetworkInterfaces = networkItems; + // Store the last selected id + var id = SelectedNetworkInterface?.Id ?? string.Empty; + + // Get all network interfaces... + var networkInterfaces = await NetworkInterface.GetNetworkInterfacesAsync(); + + // Invoke on UI thread synchronously + Application.Current.Dispatcher.Invoke(() => + { + // Clear the list + NetworkInterfaces.Clear(); + + // Add all network interfaces to the list + networkInterfaces.ForEach(NetworkInterfaces.Add); + }); + + // Set the last selected interface, if it is still available on this machine... + SelectedNetworkInterface = string.IsNullOrEmpty(id) + ? NetworkInterfaces.FirstOrDefault() + : NetworkInterfaces.FirstOrDefault(x => x.Id == id); + IsNetworkInterfaceLoading = false; } diff --git a/Website/docs/changelog/next-release.md b/Website/docs/changelog/next-release.md index dbf1688fa2..6c2385f741 100644 --- a/Website/docs/changelog/next-release.md +++ b/Website/docs/changelog/next-release.md @@ -27,7 +27,7 @@ Release date: **xx.xx.2025** **PuTTY** -- Find `putty.exe` and `powershell.exe` executable by path, similar to `where.exe`. [#2962](https://github.com/BornToBeRoot/NETworkManager/pull/2962) +- Find `putty.exe` executable by path, similar to `where.exe`. [#2962](https://github.com/BornToBeRoot/NETworkManager/pull/2962) **AWS Session Manager** @@ -35,6 +35,10 @@ Release date: **xx.xx.2025** ## Bugfixes +**Network Interface** + +- Re-select the network interface after a network change or configuration update. Thanks to [@Ghislain1](https://github.com/Ghislain1) [#3004](https://github.com/BornToBeRoot/NETworkManager/pull/3004) [#2962](https://github.com/BornToBeRoot/NETworkManager/pull/2962) + ## Dependencies, Refactoring & Documentation - Code cleanup & refactoring