@@ -23,6 +23,8 @@ namespace Files.App.Storage
2323 /// </remarks>
2424 public unsafe class JumpListManager : IDisposable
2525 {
26+ private static readonly Lock _updateJumpListLock = new ( ) ;
27+
2628 private string _aumid = null ! ;
2729 private string _exeAlias = null ! ;
2830 private string _recentCategoryName = null ! ;
@@ -36,14 +38,14 @@ public unsafe class JumpListManager : IDisposable
3638
3739 private JumpListManager ( ) { }
3840
39- public static JumpListManager ? Create ( string amuid , string exeAlias )
41+ public static JumpListManager ? Create ( string aumid , string exeAlias )
4042 {
4143 var categoryName = WindowsStorableHelpers . ResolveIndirectString ( $ "@{{{WindowsStorableHelpers.GetEnvironmentVariable(" SystemRoot")}\\ SystemResources\\ Windows.UI.ShellCommon\\ Windows.UI.ShellCommon.pri? ms-resource://Windows.UI.ShellCommon/JumpViewUI/JumpViewCategoryType_Recent}}" ) ;
4244 if ( categoryName is null ) return null ;
4345
4446 return new JumpListManager ( )
4547 {
46- _aumid = amuid ,
48+ _aumid = aumid ,
4749 _exeAlias = exeAlias ,
4850 _recentCategoryName = categoryName ,
4951 } ;
@@ -111,12 +113,12 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
111113 if ( FAILED ( hr ) ) continue ;
112114
113115 // Get an instance of IShellLinkW from the IShellItem instance
114- IShellLinkW * psl = default ;
115- hr = CreateLinkFromItem ( psi . Get ( ) , & psl ) ;
116+ using ComPtr < IShellLinkW > psl = default ;
117+ hr = CreateLinkFromItem ( psi . Get ( ) , psl . GetAddressOf ( ) ) ;
116118 if ( FAILED ( hr ) ) continue ;
117119
118120 // Pin it to the Files' Automatic Destinations
119- hr = pFilesADL . Get ( ) ->PinItem ( ( IUnknown * ) psl , pinIndex ) ;
121+ hr = pFilesADL . Get ( ) ->PinItem ( ( IUnknown * ) psl . Get ( ) , pinIndex ) ;
120122 if ( FAILED ( hr ) ) continue ;
121123 }
122124
@@ -147,12 +149,12 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
147149 if ( SUCCEEDED ( hr ) ) continue ; // If not pinned, HRESULT is E_NOT_SET
148150
149151 // Get an instance of IShellLinkW from the IShellItem instance
150- IShellLinkW * psl = default ;
151- hr = CreateLinkFromItem ( psi . Get ( ) , & psl ) ;
152+ using ComPtr < IShellLinkW > psl = default ;
153+ hr = CreateLinkFromItem ( psi . Get ( ) , psl . GetAddressOf ( ) ) ;
152154 if ( FAILED ( hr ) ) continue ;
153155
154156 // Add it to the Files' Custom Destinations
155- hr = pNewObjectCollection . Get ( ) ->AddObject ( ( IUnknown * ) psl ) ;
157+ hr = pNewObjectCollection . Get ( ) ->AddObject ( ( IUnknown * ) psl . Get ( ) ) ;
156158 if ( FAILED ( hr ) ) continue ;
157159 }
158160
@@ -171,7 +173,7 @@ public HRESULT PullJumpListFromExplorer(int maxCount = 200)
171173 if ( FAILED ( hr ) ) return hr ;
172174
173175 // Append "Recent" category
174- hr = pFilesCDL . Get ( ) ->AppendCategory ( ( PCWSTR ) Unsafe . AsPointer ( ref Unsafe . AsRef ( in "Recent" . GetPinnableReference ( ) ) ) , pNewObjectArray . Get ( ) ) ;
176+ hr = pFilesCDL . Get ( ) ->AppendCategory ( ( PCWSTR ) Unsafe . AsPointer ( ref Unsafe . AsRef ( in _recentCategoryName . GetPinnableReference ( ) ) ) , pNewObjectArray . Get ( ) ) ;
175177 if ( FAILED ( hr ) ) return hr ;
176178
177179 // Commit the collection updates
@@ -224,7 +226,8 @@ public HRESULT PushJumpListToExplorer(int maxCount = 200)
224226
225227 // Get the count of categories in the Files' Custom Destinations
226228 uint count = 0U ;
227- pFilesICDL . Get ( ) ->GetCategoryCount ( & count ) ;
229+ hr = pFilesICDL . Get ( ) ->GetCategoryCount ( & count ) ;
230+ if ( FAILED ( hr ) ) return hr ;
228231
229232 // Find the "Recent" category index
230233 uint indexOfRecentCategory = 0U ;
@@ -254,6 +257,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
254257 // Get the items in the "Recent" category
255258 using ComPtr < IObjectCollection > poc = default ;
256259 hr = pFilesICDL . Get ( ) ->EnumerateCategoryDestinations ( indexOfRecentCategory , IID . IID_IObjectCollection , ( void * * ) poc . GetAddressOf ( ) ) ;
260+ if ( FAILED ( hr ) ) return hr ;
257261
258262 // Get the count of items in the "Recent" category
259263 uint countOfItems = 0U ;
@@ -274,6 +278,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
274278 using ComHeapPtr < char > pszParseablePath = default ;
275279 pszParseablePath . Allocate ( PInvoke . MAX_PATH ) ;
276280 hr = psl . Get ( ) ->GetArguments ( pszParseablePath . Get ( ) , ( int ) PInvoke . MAX_PATH ) ;
281+ if ( FAILED ( hr ) ) continue ;
277282
278283 using ComHeapPtr < IShellItem > psi = default ;
279284 hr = PInvoke . SHCreateItemFromParsingName ( pszParseablePath . Get ( ) , null , IID . IID_IShellItem , ( void * * ) psi . GetAddressOf ( ) ) ;
@@ -308,6 +313,7 @@ category.Type is not APPDESTCATEGORYTYPE.CUSTOM ||
308313 using ComHeapPtr < char > pszParseablePath = default ;
309314 pszParseablePath . Allocate ( PInvoke . MAX_PATH ) ;
310315 hr = psl . Get ( ) ->GetArguments ( pszParseablePath . Get ( ) , ( int ) PInvoke . MAX_PATH ) ;
316+ if ( FAILED ( hr ) ) continue ;
311317
312318 using ComHeapPtr < IShellItem > psi = default ;
313319 hr = PInvoke . SHCreateItemFromParsingName ( pszParseablePath . Get ( ) , null , IID . IID_IShellItem , ( void * * ) psi . GetAddressOf ( ) ) ;
@@ -400,8 +406,26 @@ public bool WatchJumpListChanges(string aumidCrcHash)
400406 }
401407 catch
402408 {
403- // Gracefully exit if we can't monitor the file
404- return false ;
409+ if ( _explorerADLStoreFileWatcher is not null )
410+ {
411+ _explorerADLStoreFileWatcher . EnableRaisingEvents = false ;
412+ _explorerADLStoreFileWatcher . Changed -= ExplorerJumpListWatcher_Changed ;
413+ _explorerADLStoreFileWatcher . Dispose ( ) ;
414+ }
415+
416+ if ( _filesADLStoreFileWatcher is not null )
417+ {
418+ _filesADLStoreFileWatcher . EnableRaisingEvents = false ;
419+ _filesADLStoreFileWatcher . Changed -= FilesJumpListWatcher_Changed ;
420+ _filesADLStoreFileWatcher . Dispose ( ) ;
421+ }
422+
423+ if ( _filesCDLStoreFileWatcher is not null )
424+ {
425+ _filesCDLStoreFileWatcher . EnableRaisingEvents = false ;
426+ _filesCDLStoreFileWatcher . Changed -= FilesJumpListWatcher_Changed ;
427+ _filesCDLStoreFileWatcher . Dispose ( ) ;
428+ }
405429 }
406430
407431 return true ;
@@ -491,33 +515,42 @@ private HRESULT GetFolderIconLocation(IShellItem* psi, PWSTR* pIconFilePath, uin
491515
492516 private void ExplorerJumpListWatcher_Changed ( object sender , FileSystemEventArgs e )
493517 {
494- ExplorerJumpListChanged ? . Invoke ( this , EventArgs . Empty ) ;
495- PullJumpListFromExplorer ( ) ;
518+ lock ( _updateJumpListLock )
519+ {
520+ ExplorerJumpListChanged ? . Invoke ( this , EventArgs . Empty ) ;
521+ PullJumpListFromExplorer ( ) ;
522+ }
496523 }
497524
498525 private void FilesJumpListWatcher_Changed ( object sender , FileSystemEventArgs e )
499526 {
500- FilesJumpListChanged ? . Invoke ( this , EventArgs . Empty ) ;
501- PushJumpListToExplorer ( ) ;
527+ lock ( _updateJumpListLock )
528+ {
529+ FilesJumpListChanged ? . Invoke ( this , EventArgs . Empty ) ;
530+ PushJumpListToExplorer ( ) ;
531+ }
502532 }
503533
504534 public void Dispose ( )
505535 {
506536 if ( _explorerADLStoreFileWatcher is not null )
507537 {
508538 _explorerADLStoreFileWatcher . EnableRaisingEvents = false ;
539+ _explorerADLStoreFileWatcher . Changed -= ExplorerJumpListWatcher_Changed ;
509540 _explorerADLStoreFileWatcher . Dispose ( ) ;
510541 }
511542
512543 if ( _filesADLStoreFileWatcher is not null )
513544 {
514545 _filesADLStoreFileWatcher . EnableRaisingEvents = false ;
546+ _filesADLStoreFileWatcher . Changed -= FilesJumpListWatcher_Changed ;
515547 _filesADLStoreFileWatcher . Dispose ( ) ;
516548 }
517549
518550 if ( _filesCDLStoreFileWatcher is not null )
519551 {
520552 _filesCDLStoreFileWatcher . EnableRaisingEvents = false ;
553+ _filesCDLStoreFileWatcher . Changed -= FilesJumpListWatcher_Changed ;
521554 _filesCDLStoreFileWatcher . Dispose ( ) ;
522555 }
523556 }
0 commit comments