From 4aaca4d19f91030d12418c7d4399150719c77a61 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Sat, 23 Dec 2023 18:51:41 +0800 Subject: [PATCH] fix reentrant issue --- .../Snap.Hutao/Core/Threading/Throttler.cs | 19 ------------------- .../Service/User/UserCollectionService.cs | 13 ++++++++++--- .../Snap.Hutao/View/Page/SettingPage.xaml | 12 ++++++------ 3 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Threading/Throttler.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Threading/Throttler.cs b/src/Snap.Hutao/Snap.Hutao/Core/Threading/Throttler.cs deleted file mode 100644 index 1b12e79e..00000000 --- a/src/Snap.Hutao/Snap.Hutao/Core/Threading/Throttler.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) DGP Studio. All rights reserved. -// Licensed under the MIT license. - -using System.Collections.Concurrent; -using System.Runtime.CompilerServices; - -namespace Snap.Hutao.Core.Threading; - -internal sealed class Throttler -{ - private readonly ConcurrentDictionary methodSemaphoreMap = new(); - - public ValueTask ThrottleAsync(CancellationToken token = default, [CallerMemberName] string callerName = default!, [CallerLineNumber] int callerLine = 0) - { - string key = $"{callerName}L{callerLine}"; - SemaphoreSlim semaphore = methodSemaphoreMap.GetOrAdd(key, name => new SemaphoreSlim(1)); - return semaphore.EnterAsync(token); - } -} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs b/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs index d059b1fb..bce34ac7 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs @@ -14,7 +14,7 @@ namespace Snap.Hutao.Service.User; [ConstructorGenerated] [Injection(InjectAs.Singleton, typeof(IUserCollectionService))] -internal sealed partial class UserCollectionService : IUserCollectionService +internal sealed partial class UserCollectionService : IUserCollectionService, IDisposable { private readonly ScopedDbCurrent dbCurrent; private readonly IUserInitializationService userInitializationService; @@ -22,7 +22,7 @@ internal sealed partial class UserCollectionService : IUserCollectionService private readonly ITaskContext taskContext; private readonly IMessenger messenger; - private readonly Throttler throttler = new(); + private readonly SemaphoreSlim throttler = new(1); private ObservableCollection? userCollection; private Dictionary? midUserMap; @@ -38,7 +38,9 @@ internal sealed partial class UserCollectionService : IUserCollectionService public async ValueTask> GetUserCollectionAsync() { - using (await throttler.ThrottleAsync().ConfigureAwait(false)) + // Force run in background thread + await taskContext.SwitchToBackgroundAsync(); + using (await throttler.EnterAsync().ConfigureAwait(false)) { if (userCollection is null) { @@ -176,4 +178,9 @@ internal sealed partial class UserCollectionService : IUserCollectionService ArgumentNullException.ThrowIfNull(newUser.UserInfo); return new(UserOptionResult.Added, newUser.UserInfo.Uid); } + + public void Dispose() + { + throttler.Dispose(); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index 3c118612..6de22392 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -136,17 +136,17 @@ Background="{ThemeResource SystemFillColorSuccessBackgroundBrush}" Description="{shcm:ResourceString Name=ViewPageSettingHutaoPassportLicensedDeveloperDescription}" Header="{shcm:ResourceString Name=ViewPageSettingHutaoPassportLicensedDeveloperHeader}" - Visibility="{Binding UserOptions.IsLicensedDeveloper, Converter={StaticResource BoolToVisibilityConverter}}"/> - + Visibility="{Binding UserOptions.IsLicensedDeveloper, Converter={StaticResource BoolToVisibilityConverter}}">