@@ -19,6 +19,7 @@ private class CachedMediaPlayerInfo
1919 public required string Title { get ; set ; }
2020 public ImageSource ? Icon { get ; set ; }
2121 public int ProcessId { get ; set ; }
22+ public required DateTime ProcessIdCheckedAt { get ; set ; }
2223 }
2324 // cache for media player info to avoid redundant process lookups
2425 private static readonly ConcurrentDictionary < string , CachedMediaPlayerInfo > mediaPlayerCache = [ ] ;
@@ -119,7 +120,8 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
119120 {
120121 Title = shellTitle ,
121122 Icon = shellIcon ,
122- ProcessId = - 1 // no process match, -1 avoids colliding with real PIDs
123+ ProcessId = - 1 , // no process match, -1 avoids colliding with real PIDs
124+ ProcessIdCheckedAt = DateTime . UtcNow
123125 } ;
124126
125127 return ( shellTitle , shellIcon ) ;
@@ -141,7 +143,8 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
141143 {
142144 Title = mediaTitle ,
143145 Icon = mediaIcon ,
144- ProcessId = processData . ProcessId
146+ ProcessId = processData . ProcessId ,
147+ ProcessIdCheckedAt = DateTime . UtcNow
145148 } ;
146149
147150 return ( mediaTitle , mediaIcon ) ;
@@ -151,9 +154,35 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
151154 {
152155 GetAndCacheMediaPlayerData ( mediaPlayerId ) ;
153156
154- if ( ! mediaPlayerCache . TryGetValue ( mediaPlayerId , out var cachedInfo )
155- && ( ! mediaPlayerIdVariants . TryGetValue ( mediaPlayerId , out var variantKey )
156- || ! mediaPlayerCache . TryGetValue ( variantKey , out cachedInfo ) ) ) return null ;
157+ string cacheKey ;
158+ if ( mediaPlayerCache . TryGetValue ( mediaPlayerId , out var cachedInfo ) )
159+ {
160+ cacheKey = mediaPlayerId ;
161+ }
162+ else if ( mediaPlayerIdVariants . TryGetValue ( mediaPlayerId , out var variantKey )
163+ && mediaPlayerCache . TryGetValue ( variantKey , out cachedInfo ) )
164+ {
165+ cacheKey = variantKey ;
166+ }
167+ else
168+ {
169+ return null ;
170+ }
171+
172+ if ( cachedInfo . ProcessId > 0 )
173+ return cachedInfo . ProcessId ;
174+
175+ if ( DateTime . UtcNow - cachedInfo . ProcessIdCheckedAt < TimeSpan . FromSeconds ( CACHE_DURATION_SECONDS ) )
176+ return null ;
177+
178+ mediaPlayerCache . TryRemove ( cacheKey , out _ ) ;
179+ mediaPlayerIdVariants . TryRemove ( mediaPlayerId , out _ ) ;
180+ GetAndCacheMediaPlayerData ( mediaPlayerId ) ;
181+
182+ if ( ! mediaPlayerCache . TryGetValue ( mediaPlayerId , out cachedInfo )
183+ && ( ! mediaPlayerIdVariants . TryGetValue ( mediaPlayerId , out var refreshedVariantKey )
184+ || ! mediaPlayerCache . TryGetValue ( refreshedVariantKey , out cachedInfo ) ) )
185+ return null ;
157186
158187 return cachedInfo . ProcessId > 0 ? cachedInfo . ProcessId : null ;
159188 }
@@ -286,7 +315,8 @@ private static bool IsBrowser(string processName) =>
286315 {
287316 Title = title ,
288317 Icon = icon ,
289- ProcessId = processId
318+ ProcessId = processId ,
319+ ProcessIdCheckedAt = DateTime . UtcNow
290320 } ;
291321 }
292322
0 commit comments