Skip to content

Commit 9321a7d

Browse files
committed
Fix program lock issue
1 parent 51df66e commit 9321a7d

File tree

2 files changed

+79
-35
lines changed

2 files changed

+79
-35
lines changed

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

Lines changed: 41 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}>");
@@ -408,38 +419,47 @@ private static async Task DisableProgramAsync(IProgram programToDelete)
408419
return;
409420

410421
await _uwpsLock.WaitAsync();
411-
if (_uwps.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
422+
var reindexUwps = true;
423+
try
412424
{
425+
reindexUwps = _uwps.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
413426
var program = _uwps.First(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
414427
program.Enabled = false;
415428
_settings.DisabledProgramSources.Add(new ProgramSource(program));
429+
}
430+
finally
431+
{
416432
_uwpsLock.Release();
433+
}
417434

418-
// Reindex UWP programs
435+
// Reindex UWP programs
436+
if (reindexUwps)
437+
{
419438
_ = Task.Run(IndexUwpProgramsAsync);
420439
return;
421440
}
422-
else
423-
{
424-
_uwpsLock.Release();
425-
}
426441

427442
await _win32sLock.WaitAsync();
428-
if (_win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
443+
var reindexWin32s = true;
444+
try
429445
{
446+
reindexWin32s = _win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
430447
var program = _win32s.First(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier);
431448
program.Enabled = false;
432449
_settings.DisabledProgramSources.Add(new ProgramSource(program));
433450
_win32sLock.Release();
434-
435-
// Reindex Win32 programs
436-
_ = Task.Run(IndexWin32ProgramsAsync);
437-
return;
438451
}
439-
else
452+
finally
440453
{
441454
_win32sLock.Release();
442455
}
456+
457+
// Reindex Win32 programs
458+
if (reindexWin32s)
459+
{
460+
_ = Task.Run(IndexWin32ProgramsAsync);
461+
return;
462+
}
443463
}
444464

445465
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)