From 01c2516eaa7c43f52dfeede6ed76136903849d52 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 22 Feb 2026 14:04:44 -0500 Subject: [PATCH] Fix the symbol cache and image cache --- PortfolioManager/Cache/ImageCache.cs | 27 ++++++++++++++++++--------- PortfolioManager/Cache/SymbolCache.cs | 20 ++++++++++++-------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/PortfolioManager/Cache/ImageCache.cs b/PortfolioManager/Cache/ImageCache.cs index ffdbb0c..6044a26 100644 --- a/PortfolioManager/Cache/ImageCache.cs +++ b/PortfolioManager/Cache/ImageCache.cs @@ -57,6 +57,7 @@ namespace PortfolioManager.Cache { lock (thisLock) { + DisposeBitmaps(); imageCache = new Dictionary(); } } @@ -66,22 +67,30 @@ namespace PortfolioManager.Cache lock (thisLock) { if (null == imageCacheInstance) return; - List bitmaps = imageCache.Values.ToList(); - foreach (Bitmap bitmap in bitmaps) - { - bitmap.Dispose(); - } + DisposeBitmaps(); imageCache = null; imageCacheInstance = null; } } - - public IImage GetImage(ImageCache.ImageType imageType) + + private void DisposeBitmaps() { - lock(this) + if(null == imageCache)return; + List bitmaps = imageCache.Values.ToList(); + foreach (Bitmap bitmap in bitmaps) { - return imageCache[imageType]; + bitmap.Dispose(); } } + + public IImage GetImage(ImageType imageType) + { + lock(thisLock) + { + if (imageCache == null)throw new ObjectDisposedException(nameof(ImageCache)); + if (!imageCache.TryGetValue(imageType, out var bitmap))throw new KeyNotFoundException($"Image {imageType} not found in cache."); + return bitmap; + } + } } } diff --git a/PortfolioManager/Cache/SymbolCache.cs b/PortfolioManager/Cache/SymbolCache.cs index f6c584c..f80bafa 100644 --- a/PortfolioManager/Cache/SymbolCache.cs +++ b/PortfolioManager/Cache/SymbolCache.cs @@ -10,7 +10,8 @@ namespace PortfolioManager.Cache public class SymbolCache : IDisposable { private List symbolCache=new List(); - private Object thisLock=new Object(); + private readonly Object thisLock=new Object(); + private readonly Object fetchLock = new Object(); private Thread cacheMonitorThread=null; private volatile bool threadRun=true; private int cacheRefreshAfter=60000; // Invalidate cache after @@ -60,12 +61,15 @@ namespace PortfolioManager.Cache { if(symbolCache.Count>0)return new List(symbolCache); } - List symbols = PricingDA.GetSymbols(); - lock(thisLock) + lock(fetchLock) { - if(symbolCache.Count>0)return new List(symbolCache); - symbolCache=new List(symbols); - return new List(symbols); + List symbols = PricingDA.GetSymbols(); + lock(thisLock) + { + if(symbolCache.Count>0)return new List(symbolCache); + symbolCache=new List(symbols); + return new List(symbols); + } } } @@ -81,7 +85,7 @@ namespace PortfolioManager.Cache if(quantums>cacheRefreshAfter) { quantums=0; - List symbols = PricingDA.GetSymbols(); + List symbols = PricingDA.GetSymbols(); lock(thisLock) { symbolCache=new List(symbols); @@ -90,4 +94,4 @@ namespace PortfolioManager.Cache } } } -} +} \ No newline at end of file