Skip to content

Commit ad2854f

Browse files
committed
Avoid race condition
1 parent 90b072a commit ad2854f

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

src/Files.App.Storage/Windows/Managers/JumpListManager.cs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public unsafe class JumpListManager : IDisposable
3131
private FileSystemWatcher? _filesADLStoreFileWatcher;
3232
private FileSystemWatcher? _filesCDLStoreFileWatcher;
3333

34+
private readonly Lock _updatJumpListLock = new();
35+
3436
public static event EventHandler? ExplorerJumpListChanged;
3537
public static event EventHandler? FilesJumpListChanged;
3638

@@ -399,7 +401,27 @@ public bool WatchJumpListChanges(string aumidCrcHash)
399401
}
400402
catch
401403
{
402-
// Gracefully exit if we can't monitor the file
404+
if (_explorerADLStoreFileWatcher is not null)
405+
{
406+
_explorerADLStoreFileWatcher.EnableRaisingEvents = false;
407+
_explorerADLStoreFileWatcher.Changed -= ExplorerJumpListWatcher_Changed;
408+
_explorerADLStoreFileWatcher.Dispose();
409+
}
410+
411+
if (_filesADLStoreFileWatcher is not null)
412+
{
413+
_filesADLStoreFileWatcher.EnableRaisingEvents = false;
414+
_filesADLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
415+
_filesADLStoreFileWatcher.Dispose();
416+
}
417+
418+
if (_filesCDLStoreFileWatcher is not null)
419+
{
420+
_filesCDLStoreFileWatcher.EnableRaisingEvents = false;
421+
_filesCDLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
422+
_filesCDLStoreFileWatcher.Dispose();
423+
}
424+
403425
return false;
404426
}
405427

@@ -490,31 +512,66 @@ private HRESULT GetFolderIconLocation(IShellItem* psi, PWSTR* pIconFilePath, uin
490512

491513
private void ExplorerJumpListWatcher_Changed(object sender, FileSystemEventArgs e)
492514
{
493-
PullJumpListFromExplorer();
515+
STATask.Run(() =>
516+
{
517+
if (_updatJumpListLock.TryEnter()) // do not wait
518+
{
519+
try
520+
{
521+
Debug.WriteLine("in: ExplorerJumpListWatcher_Changed");
522+
PullJumpListFromExplorer();
523+
Debug.WriteLine("out: ExplorerJumpListWatcher_Changed");
524+
}
525+
finally
526+
{
527+
_updatJumpListLock.Exit();
528+
}
529+
}
530+
},
531+
null);
494532
}
495533

496534
private void FilesJumpListWatcher_Changed(object sender, FileSystemEventArgs e)
497535
{
498-
PushJumpListToExplorer();
536+
STATask.Run(() =>
537+
{
538+
if (_updatJumpListLock.TryEnter()) // do not wait
539+
{
540+
try
541+
{
542+
Debug.WriteLine("in: FilesJumpListWatcher_Changed");
543+
PushJumpListToExplorer();
544+
Debug.WriteLine("out: FilesJumpListWatcher_Changed");
545+
}
546+
finally
547+
{
548+
_updatJumpListLock.Exit();
549+
}
550+
}
551+
},
552+
null);
499553
}
500554

501555
public void Dispose()
502556
{
503557
if (_explorerADLStoreFileWatcher is not null)
504558
{
505559
_explorerADLStoreFileWatcher.EnableRaisingEvents = false;
560+
_explorerADLStoreFileWatcher.Changed -= ExplorerJumpListWatcher_Changed;
506561
_explorerADLStoreFileWatcher.Dispose();
507562
}
508563

509564
if (_filesADLStoreFileWatcher is not null)
510565
{
511566
_filesADLStoreFileWatcher.EnableRaisingEvents = false;
567+
_filesADLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
512568
_filesADLStoreFileWatcher.Dispose();
513569
}
514570

515571
if (_filesCDLStoreFileWatcher is not null)
516572
{
517573
_filesCDLStoreFileWatcher.EnableRaisingEvents = false;
574+
_filesCDLStoreFileWatcher.Changed -= FilesJumpListWatcher_Changed;
518575
_filesCDLStoreFileWatcher.Dispose();
519576
}
520577
}

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
using Sentry;
1313
using Sentry.Protocol;
1414
using System.IO;
15-
using System.Runtime.InteropServices;
1615
using System.Text;
1716
using Windows.ApplicationModel;
18-
using Windows.Media.Playback;
1917
using Windows.Storage;
2018
using Windows.System;
2119
using Windows.Win32.Foundation;
@@ -169,7 +167,7 @@ await Task.WhenAll(
169167
if (JumpListManager is not null)
170168
{
171169
HRESULT hr = JumpListManager.PullJumpListFromExplorer();
172-
if (hr.Failed) App.Logger.LogWarning("Failed to synchronizing jump list unexpectedly.");
170+
if (hr.Failed) App.Logger.LogWarning("Failed to synchronize jump list unexpectedly.");
173171

174172
bool result = JumpListManager.WatchJumpListChanges(AppUserModelIdCrcHash);
175173
if (!result) App.Logger.LogWarning("Failed to watch jump list unexpectedly.");

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,13 @@ public async Task SetWorkingDirectoryAsync(string? value)
228228
if (value == "Home" || value == "ReleaseNotes" || value == "Settings")
229229
currentStorageFolder = null;
230230
else
231-
AppLifecycleHelper.JumpListManager?.AddFolderToRecentCategory(value);
231+
{
232+
_ = STATask.Run(() =>
233+
{
234+
AppLifecycleHelper.JumpListManager?.AddFolderToRecentCategory(value);
235+
},
236+
App.Logger);
237+
}
232238

233239
WorkingDirectory = value;
234240

0 commit comments

Comments
 (0)