public BitmapSource GetBitmapSource(FrameKey frameKey, CancellationTokenSource cancellationTokenSource) { //Console.WriteLine("GetBitmapSource internal START " + frameKey.ToString()); BitmapSource bitmapSource = null; WeakReference weakReference = null; if (m_FrameBitmapSources.TryGetValue(frameKey.ToString(), out weakReference) == true) { bitmapSource = weakReference.Target as BitmapSource; //Console.WriteLine("GetBitmapSource internal, already contains bitmap source frame key (but perhaps weak reference is dead) " + frameKey.ToString()); //Console.WriteLine(weakReference.IsAlive ? "hashed bitmap source is Alive" : "bitmap source Died"); } if (bitmapSource == null) //means either weak reference was not found, or was found but target bitmapsource died { bitmapSource = GetTiffBitmapSource(frameKey.FileName, frameKey.FrameIndex, cancellationTokenSource); if (weakReference == null) //if earlier attempt to find key failed { //if (m_FrameBitmapSources.TryGetValue(frameKey.ToString(), out weakReference) == false) //{ m_FrameBitmapSources.TryAdd(frameKey.ToString(), weakReference = new WeakReference(bitmapSource)); //} } weakReference.Target = bitmapSource; } m_FrameBitmapSourcesToForceKeep.AddOrUpdate(frameKey.ToString(), bitmapSource, (k, x) => x == null ? bitmapSource : x); SetScore(frameKey, long.MaxValue); //Console.WriteLine("GetBitmapSource internal END " + frameKey.ToString()); return(bitmapSource); }
public RenderTargetBitmap GetRenderTargetBitmap(FrameKey frameKey, CancellationTokenSource cancellationTokenSource) { //Console.WriteLine("GetRenderTargetBitmap internal START " + frameKey.ToString()); RenderTargetBitmap renderTargetBitmap = null; WeakReference weakReference = null; if (m_FrameRenderTargetBitmaps.TryGetValue(frameKey.ToStringExt(), out weakReference) == true) { renderTargetBitmap = weakReference.Target as RenderTargetBitmap; //Console.WriteLine("GetBitmapSource internal, already contains bitmap source frame key (but perhaps weak reference is dead) " + frameKey.ToString()); //Console.WriteLine(weakReference.IsAlive ? "hashed bitmap source is Alive" : "bitmap source Died"); } if (renderTargetBitmap == null) //means either weak reference was not found, or was found but target bitmapsource died { renderTargetBitmap = GetNewRenderTargetBitmap(frameKey, cancellationTokenSource); if (weakReference == null) //if earlier attempt to find key failed { //if (m_FrameBitmapSources.TryGetValue(frameKey.ToString(), out weakReference) == false) //{ m_FrameRenderTargetBitmaps.TryAdd(frameKey.ToStringExt(), weakReference = new WeakReference(renderTargetBitmap)); m_RenderTargetBitmapInstances .AddOrUpdate( frameKey.ToString(), new List <string>() { frameKey.ToStringExt() }, (k, x) => { x.Add(frameKey.ToStringExt()); return(x); }); //} } weakReference.Target = renderTargetBitmap; } m_FrameRenderTargetBitmapsToForceKeep .AddOrUpdate(frameKey.ToStringExt(), renderTargetBitmap, (k, x) => x == null ? renderTargetBitmap : x); //SetScore(frameKey, long.MaxValue); //no need to set score here, it is set when retrieving the bitmap source //Console.WriteLine("GetRenderTargetBitmap internal END " + frameKey.ToString()); return(renderTargetBitmap); }
public void SetScore(FrameKey frameKey, long ticks) { m_FrameScores.AddOrUpdate(frameKey.ToString(), ticks, (k, x) => ticks); }