@@ -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 }
0 commit comments