Improve performance, thread safety, and resource handling

Enhanced monitor initialization with parallelism in `MonitorManager.cs` for better performance. Added cancellation support to `NativeEventWaiter.cs` with `CancellationToken` and timeout handling. Introduced thread safety in `PhysicalMonitorHandleManager.cs` using locks to prevent race conditions.

Updated `PowerDisplayViewModel.cs` to include proper resource cleanup with `CancellationTokenSource` and improved memory management. Added necessary namespaces for threading and asynchronous operations. General code improvements for readability, maintainability, and reliability.
This commit is contained in:
Yu Leng
2025-11-19 15:08:00 +08:00
parent f10c9f49e9
commit ad83b5e67f
4 changed files with 54 additions and 28 deletions

View File

@@ -10,6 +10,7 @@ using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
@@ -72,7 +73,8 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
Logger.LogInfo("Received refresh monitors event from PowerDisplay.exe");
ReloadMonitorsFromSettings();
});
},
_cancellationTokenSource.Token);
}
private void InitializeEnabledValue()
@@ -284,6 +286,10 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_monitors.CollectionChanged -= Monitors_CollectionChanged;
}
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
GC.SuppressFinalize(this);
base.Dispose();
}
@@ -447,6 +453,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private PowerDisplaySettings _settings;
private ObservableCollection<MonitorInfo> _monitors;
private bool _hasMonitors;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
public void RefreshEnabledState()
{