Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Core / CSharp / System / Windows / Media / Imaging / ImagingCache.cs / 1 / ImagingCache.cs
//------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation // // File: ImagingCache.cs // //----------------------------------------------------------------------------- using System; using System.IO; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Reflection; using MS.Internal; using System.Diagnostics; using System.Windows.Media; using System.Globalization; using System.Security; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Windows.Media.Animation; using System.Windows.Media.Composition; using System.Windows.Media.Imaging; using MS.Win32; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace System.Windows.Media.Imaging { #region ImagingCache /// /// ImagingCache provides caching for different Imaging objects /// Caches are thread-safe. /// internal static class ImagingCache { #region Methods /// Adds an object to the image cache internal static void AddToImageCache(Uri uri, object obj) { AddToCache(uri, obj, _imageCache); } /// Removes an object from the image cache internal static void RemoveFromImageCache(Uri uri) { RemoveFromCache(uri, _imageCache); } /// Get object from the image cache internal static object CheckImageCache(Uri uri) { return CheckCache(uri, _imageCache); } /// Adds an object to the decoder cache internal static void AddToDecoderCache(Uri uri, object obj) { AddToCache(uri, obj, _decoderCache); } /// Removes an object from the decoder cache internal static void RemoveFromDecoderCache(Uri uri) { RemoveFromCache(uri, _decoderCache); } /// Get object from the image cache internal static object CheckDecoderCache(Uri uri) { return CheckCache(uri, _decoderCache); } /// Adds an object to a given table private static void AddToCache(Uri uri, object obj, Hashtable table) { lock(table) { // if entry is already there, exit if (table.Contains(uri)) { return; } // if the table has reached the max size, try to see if we can reduce its size if (table.Count == MAX_CACHE_SIZE) { ArrayList al = new ArrayList(); foreach (DictionaryEntry de in table) { // if the value is a WeakReference that has been GC'd, remove it WeakReference weakRef = de.Value as WeakReference; if ((weakRef != null) && (weakRef.Target == null)) { al.Add(de.Key); } } foreach (object o in al) { table.Remove(o); } } // if table is still maxed out, exit if (table.Count == MAX_CACHE_SIZE) { return; } // add it table[uri] = obj; } } /// Removes an object from a given table private static void RemoveFromCache(Uri uri, Hashtable table) { lock(table) { // if entry is there, remove it if (table.Contains(uri)) { table.Remove(uri); } } } /// Return an object from a given table private static object CheckCache(Uri uri, Hashtable table) { lock(table) { return table[uri]; } } #endregion #region Data Members /// image cache private static Hashtable _imageCache = new Hashtable(); /// decoder cache private static Hashtable _decoderCache = new Hashtable(); /// max size to limit the cache private static int MAX_CACHE_SIZE = 300; #endregion } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation // // File: ImagingCache.cs // //----------------------------------------------------------------------------- using System; using System.IO; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Reflection; using MS.Internal; using System.Diagnostics; using System.Windows.Media; using System.Globalization; using System.Security; using System.Security.Permissions; using System.Runtime.InteropServices; using System.Windows.Media.Animation; using System.Windows.Media.Composition; using System.Windows.Media.Imaging; using MS.Win32; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; namespace System.Windows.Media.Imaging { #region ImagingCache /// /// ImagingCache provides caching for different Imaging objects /// Caches are thread-safe. /// internal static class ImagingCache { #region Methods /// Adds an object to the image cache internal static void AddToImageCache(Uri uri, object obj) { AddToCache(uri, obj, _imageCache); } /// Removes an object from the image cache internal static void RemoveFromImageCache(Uri uri) { RemoveFromCache(uri, _imageCache); } /// Get object from the image cache internal static object CheckImageCache(Uri uri) { return CheckCache(uri, _imageCache); } /// Adds an object to the decoder cache internal static void AddToDecoderCache(Uri uri, object obj) { AddToCache(uri, obj, _decoderCache); } /// Removes an object from the decoder cache internal static void RemoveFromDecoderCache(Uri uri) { RemoveFromCache(uri, _decoderCache); } /// Get object from the image cache internal static object CheckDecoderCache(Uri uri) { return CheckCache(uri, _decoderCache); } /// Adds an object to a given table private static void AddToCache(Uri uri, object obj, Hashtable table) { lock(table) { // if entry is already there, exit if (table.Contains(uri)) { return; } // if the table has reached the max size, try to see if we can reduce its size if (table.Count == MAX_CACHE_SIZE) { ArrayList al = new ArrayList(); foreach (DictionaryEntry de in table) { // if the value is a WeakReference that has been GC'd, remove it WeakReference weakRef = de.Value as WeakReference; if ((weakRef != null) && (weakRef.Target == null)) { al.Add(de.Key); } } foreach (object o in al) { table.Remove(o); } } // if table is still maxed out, exit if (table.Count == MAX_CACHE_SIZE) { return; } // add it table[uri] = obj; } } /// Removes an object from a given table private static void RemoveFromCache(Uri uri, Hashtable table) { lock(table) { // if entry is there, remove it if (table.Contains(uri)) { table.Remove(uri); } } } /// Return an object from a given table private static object CheckCache(Uri uri, Hashtable table) { lock(table) { return table[uri]; } } #endregion #region Data Members /// image cache private static Hashtable _imageCache = new Hashtable(); /// decoder cache private static Hashtable _decoderCache = new Hashtable(); /// max size to limit the cache private static int MAX_CACHE_SIZE = 300; #endregion } #endregion } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- assemblycache.cs
- SqlClientPermission.cs
- SqlXmlStorage.cs
- Vector.cs
- AppSettingsExpressionBuilder.cs
- GlobalizationAssembly.cs
- ObjectItemCollection.cs
- DataBinder.cs
- GridItemProviderWrapper.cs
- HostingEnvironment.cs
- LocalFileSettingsProvider.cs
- EntityException.cs
- HostingEnvironmentException.cs
- CompilerGeneratedAttribute.cs
- PixelShader.cs
- Int32.cs
- GridErrorDlg.cs
- SqlDataAdapter.cs
- DirtyTextRange.cs
- DbMetaDataCollectionNames.cs
- HtmlInputControl.cs
- ProxyManager.cs
- HttpRuntime.cs
- Converter.cs
- SafeNativeMethods.cs
- NetworkInterface.cs
- SystemFonts.cs
- TextHidden.cs
- SchemaNotation.cs
- TableLayoutPanelCellPosition.cs
- ResXResourceSet.cs
- NavigateEvent.cs
- ReadOnlyHierarchicalDataSourceView.cs
- TableLayoutStyle.cs
- Unit.cs
- SmiMetaDataProperty.cs
- DefaultTraceListener.cs
- SchemaSetCompiler.cs
- TemplateContainer.cs
- _RequestCacheProtocol.cs
- WorkflowItemsPresenter.cs
- EncoderNLS.cs
- XmlSchemaRedefine.cs
- SingleTagSectionHandler.cs
- LinqToSqlWrapper.cs
- MouseButtonEventArgs.cs
- MonitoringDescriptionAttribute.cs
- ImageAnimator.cs
- OptimalBreakSession.cs
- DesignConnectionCollection.cs
- DrawingCollection.cs
- ConnectionAcceptor.cs
- DbModificationCommandTree.cs
- IfAction.cs
- InterleavedZipPartStream.cs
- SBCSCodePageEncoding.cs
- DataGridRow.cs
- Script.cs
- LogExtentCollection.cs
- XPathItem.cs
- CornerRadius.cs
- NamedObject.cs
- QueryOutputWriter.cs
- ITextView.cs
- PropertyManager.cs
- NamespaceCollection.cs
- DictionaryManager.cs
- AccessedThroughPropertyAttribute.cs
- BitmapEffectState.cs
- StateBag.cs
- DecimalAnimationBase.cs
- LoginView.cs
- DocumentsTrace.cs
- SQLInt16Storage.cs
- Track.cs
- TrackingProfileDeserializationException.cs
- Stack.cs
- Stream.cs
- ElementsClipboardData.cs
- ConfigurationManager.cs
- GacUtil.cs
- DataGridViewCellStyleConverter.cs
- AssemblyAttributesGoHere.cs
- DefaultValueAttribute.cs
- SchemaImporterExtension.cs
- SqlXmlStorage.cs
- ComponentSerializationService.cs
- XmlComplianceUtil.cs
- ReferenceEqualityComparer.cs
- DataGridViewDataConnection.cs
- SpecularMaterial.cs
- Graph.cs
- UmAlQuraCalendar.cs
- Splitter.cs
- CompressedStack.cs
- SqlBuilder.cs
- RegexReplacement.cs
- ClipboardProcessor.cs
- CompilerErrorCollection.cs
- RepeaterItemEventArgs.cs