From 966bcdd5ec2a9b8bedbe66450107f34145766259 Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Sat, 28 Feb 2026 22:03:55 +0800
Subject: [PATCH] Refactor UI thread checks in Theme async methods
Refactored RefreshFrameAsync and SetBlurForWindowAsync to check Dispatcher access before invoking UI updates, ensuring thread safety and reducing unnecessary Dispatcher calls. Also removed redundant code and unused using directive.
---
Flow.Launcher.Core/Resource/Theme.cs | 49 +++++++++++++++-------------
1 file changed, 27 insertions(+), 22 deletions(-)
diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs
index df2dd878f..c67f93a33 100644
--- a/Flow.Launcher.Core/Resource/Theme.cs
+++ b/Flow.Launcher.Core/Resource/Theme.cs
@@ -11,7 +11,6 @@ using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Shell;
-using System.Windows.Threading;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
@@ -638,26 +637,29 @@ namespace Flow.Launcher.Core.Resource
///
public async Task RefreshFrameAsync()
{
- await Application.Current.Dispatcher.InvokeAsync(() =>
+ if (Application.Current?.Dispatcher.CheckAccess() != true)
{
- // Get the actual backdrop type and drop shadow effect settings
- var (backdropType, useDropShadowEffect) = GetActualValue();
+ await Application.Current?.Dispatcher.InvokeAsync(RefreshFrameAsync);
+ return;
+ }
- // Remove OS minimizing/maximizing animation
- // Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
+ // Get the actual backdrop type and drop shadow effect settings
+ var (backdropType, useDropShadowEffect) = GetActualValue();
- // The timing of adding the shadow effect should vary depending on whether the theme is transparent.
- if (BlurEnabled)
- {
- AutoDropShadow(useDropShadowEffect);
- }
- SetBlurForWindow(_settings.Theme, backdropType);
+ // Remove OS minimizing/maximizing animation
+ // Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
- if (!BlurEnabled)
- {
- AutoDropShadow(useDropShadowEffect);
- }
- }, DispatcherPriority.Render);
+ // The timing of adding the shadow effect should vary depending on whether the theme is transparent.
+ if (BlurEnabled)
+ {
+ AutoDropShadow(useDropShadowEffect);
+ }
+ SetBlurForWindow(_settings.Theme, backdropType);
+
+ if (!BlurEnabled)
+ {
+ AutoDropShadow(useDropShadowEffect);
+ }
}
///
@@ -665,13 +667,16 @@ namespace Flow.Launcher.Core.Resource
///
public async Task SetBlurForWindowAsync()
{
- await Application.Current.Dispatcher.InvokeAsync(() =>
+ if (Application.Current?.Dispatcher.CheckAccess() != true)
{
- // Get the actual backdrop type and drop shadow effect settings
- var (backdropType, _) = GetActualValue();
+ await Application.Current?.Dispatcher.InvokeAsync(RefreshFrameAsync);
+ return;
+ }
- SetBlurForWindow(_settings.Theme, backdropType);
- }, DispatcherPriority.Render);
+ // Get the actual backdrop type and drop shadow effect settings
+ var (backdropType, _) = GetActualValue();
+
+ SetBlurForWindow(_settings.Theme, backdropType);
}
///