Skip to content

Commit 61fb5a8

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#3978 from Flow-Launcher/program_locks
Fix program lock waiting issue
1 parent 2368e3f commit 61fb5a8

File tree

2 files changed

+78
-35
lines changed

2 files changed

+78
-35
lines changed

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
3131

3232
internal static PluginInitContext Context { get; private set; }
3333

34-
private static readonly List<Result> emptyResults = new();
34+
private static readonly List<Result> emptyResults = [];
3535

3636
private static readonly MemoryCacheOptions cacheOptions = new() { SizeLimit = 1560 };
3737
private static MemoryCache cache = new(cacheOptions);
@@ -84,7 +84,6 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
8484
{
8585
await _win32sLock.WaitAsync(token);
8686
await _uwpsLock.WaitAsync(token);
87-
8887
try
8988
{
9089
// Collect all UWP Windows app directories
@@ -117,7 +116,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
117116
}
118117
}, token);
119118

120-
resultList = resultList.Any() ? resultList : emptyResults;
119+
resultList = resultList.Count != 0 ? resultList : emptyResults;
121120

122121
entry.SetSize(resultList.Count);
123122
entry.SetSlidingExpiration(TimeSpan.FromHours(8));
@@ -250,14 +249,26 @@ static void MoveFile(string sourcePath, string destinationPath)
250249
}
251250

252251
await _win32sLock.WaitAsync();
253-
_win32s = await context.API.LoadCacheBinaryStorageAsync(Win32CacheName, pluginCacheDirectory, new List<Win32>());
254-
_win32sCount = _win32s.Count;
255-
_win32sLock.Release();
252+
try
253+
{
254+
_win32s = await context.API.LoadCacheBinaryStorageAsync(Win32CacheName, pluginCacheDirectory, new List<Win32>());
255+
_win32sCount = _win32s.Count;
256+
}
257+
finally
258+
{
259+
_win32sLock.Release();
260+
}
256261

257262
await _uwpsLock.WaitAsync();
258-
_uwps = await context.API.LoadCacheBinaryStorageAsync(UwpCacheName, pluginCacheDirectory, new List<UWPApp>());
259-
_uwpsCount = _uwps.Count;
260-
_uwpsLock.Release();
263+
try
264+
{
265+
_uwps = await context.API.LoadCacheBinaryStorageAsync(UwpCacheName, pluginCacheDirectory, new List<UWPApp>());
266+
_uwpsCount = _uwps.Count;
267+
}
268+
finally
269+
{
270+
_uwpsLock.Release();
271+
}
261272
});
262273
Context.API.LogInfo(ClassName, $"Number of preload win32 programs <{_win32sCount}>");
263274
Context.API.LogInfo(ClassName, $"Number of preload uwps <{_uwpsCount}>");
@@ -407,38 +418,46 @@ private static async Task DisableProgramAsync(IProgram programToDelete)
407418
return;
408419

409420
await _uwpsLock.WaitAsync();
410-
if (_uwps.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
421+
var reindexUwps = true;
422+
try
411423
{
424+
reindexUwps = _uwps.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
412425
var program = _uwps.First(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
413426
program.Enabled = false;
414427
_settings.DisabledProgramSources.Add(new ProgramSource(program));
428+
}
429+
finally
430+
{
415431
_uwpsLock.Release();
432+
}
416433

417-
// Reindex UWP programs
434+
// Reindex UWP programs
435+
if (reindexUwps)
436+
{
418437
_ = Task.Run(IndexUwpProgramsAsync);
419438
return;
420439
}
421-
else
422-
{
423-
_uwpsLock.Release();
424-
}
425440

426441
await _win32sLock.WaitAsync();
427-
if (_win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
442+
var reindexWin32s = true;
443+
try
428444
{
445+
reindexWin32s = _win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
429446
var program = _win32s.First(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
430447
program.Enabled = false;
431448
_settings.DisabledProgramSources.Add(new ProgramSource(program));
449+
}
450+
finally
451+
{
432452
_win32sLock.Release();
453+
}
433454

434-
// Reindex Win32 programs
455+
// Reindex Win32 programs
456+
if (reindexWin32s)
457+
{
435458
_ = Task.Run(IndexWin32ProgramsAsync);
436459
return;
437460
}
438-
else
439-
{
440-
_win32sLock.Release();
441-
}
442461
}
443462

444463
public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)

Plugins/Flow.Launcher.Plugin.Program/Views/Commands/ProgramSettingDisplay.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,30 @@ internal static List<ProgramSource> LoadProgramSources()
1919
internal static async Task DisplayAllProgramsAsync()
2020
{
2121
await Main._win32sLock.WaitAsync();
22-
var win32 = Main._win32s
22+
try
23+
{
24+
var win32 = Main._win32s
2325
.Where(t1 => !ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
2426
.Select(x => new ProgramSource(x));
25-
ProgramSetting.ProgramSettingDisplayList.AddRange(win32);
26-
Main._win32sLock.Release();
27+
ProgramSetting.ProgramSettingDisplayList.AddRange(win32);
28+
}
29+
finally
30+
{
31+
Main._win32sLock.Release();
32+
}
2733

2834
await Main._uwpsLock.WaitAsync();
29-
var uwp = Main._uwps
35+
try
36+
{
37+
var uwp = Main._uwps
3038
.Where(t1 => !ProgramSetting.ProgramSettingDisplayList.Any(x => x.UniqueIdentifier == t1.UniqueIdentifier))
3139
.Select(x => new ProgramSource(x));
32-
ProgramSetting.ProgramSettingDisplayList.AddRange(uwp);
33-
Main._uwpsLock.Release();
40+
ProgramSetting.ProgramSettingDisplayList.AddRange(uwp);
41+
}
42+
finally
43+
{
44+
Main._uwpsLock.Release();
45+
}
3446
}
3547

3648
internal static async Task SetProgramSourcesStatusAsync(List<ProgramSource> selectedProgramSourcesToDisable, bool status)
@@ -44,24 +56,36 @@ internal static async Task SetProgramSourcesStatusAsync(List<ProgramSource> sele
4456
}
4557

4658
await Main._win32sLock.WaitAsync();
47-
foreach (var program in Main._win32s)
59+
try
4860
{
49-
if (selectedProgramSourcesToDisable.Any(x => x.UniqueIdentifier == program.UniqueIdentifier && program.Enabled != status))
61+
foreach (var program in Main._win32s)
5062
{
51-
program.Enabled = status;
63+
if (selectedProgramSourcesToDisable.Any(x => x.UniqueIdentifier == program.UniqueIdentifier && program.Enabled != status))
64+
{
65+
program.Enabled = status;
66+
}
5267
}
5368
}
54-
Main._win32sLock.Release();
69+
finally
70+
{
71+
Main._win32sLock.Release();
72+
}
5573

5674
await Main._uwpsLock.WaitAsync();
57-
foreach (var program in Main._uwps)
75+
try
5876
{
59-
if (selectedProgramSourcesToDisable.Any(x => x.UniqueIdentifier == program.UniqueIdentifier && program.Enabled != status))
77+
foreach (var program in Main._uwps)
6078
{
61-
program.Enabled = status;
79+
if (selectedProgramSourcesToDisable.Any(x => x.UniqueIdentifier == program.UniqueIdentifier && program.Enabled != status))
80+
{
81+
program.Enabled = status;
82+
}
6283
}
6384
}
64-
Main._uwpsLock.Release();
85+
finally
86+
{
87+
Main._uwpsLock.Release();
88+
}
6589
}
6690

6791
internal static void StoreDisabledInSettings()

0 commit comments

Comments
 (0)