Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Shared / MS / Internal / WeakReferenceList.cs / 1 / WeakReferenceList.cs
using System; using System.Collections; using System.Diagnostics; #if WINDOWS_BASE using MS.Internal.WindowsBase; #elif PRESENTATION_CORE using MS.Internal.PresentationCore; #elif PRESENTATIONFRAMEWORK using MS.Internal.PresentationFramework; #elif DRT using MS.Internal.Drt; #else #error Attempt to use FriendAccessAllowedAttribute from an unknown assembly. using MS.Internal.YourAssemblyName; #endif namespace MS.Internal { ////// This is a Cached ThreadSafe ArrayList of WeakReferences. /// - When the "List" property is requested a readonly reference to the /// list is returned and a reference to the readonly list is cached. /// - If the "List" is requested again, the same cached reference is returned. /// - When the list is modified, if a readonly reference is present in the /// cache then the list is copied before it is modified and the readonly list is /// released from the cache. /// [FriendAccessAllowed] internal class WeakReferenceList : CopyOnWriteList, IEnumerable { public WeakReferenceList():base(null) { } public WeakReferenceList(object syncRoot):base(syncRoot) { } public WeakReferenceListEnumerator GetEnumerator() { return new WeakReferenceListEnumerator(List); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } ////// Add a weak reference to the List. /// Returns true if successfully added. /// Returns false if object is already on the list. /// public override bool Add(object obj) { Debug.Assert(null!=obj, "WeakReferenceList.Add() should not be passed null."); lock(base.SyncRoot) { int index = FindWeakReference(obj); // If the object is already on the list then // we are done. (return false) if(index >= 0) return false; return base.Internal_Add(new WeakReference(obj)); } } ////// Remove a weak reference to the List. /// Returns true if successfully added. /// Returns false if object is already on the list. /// public override bool Remove(object obj) { Debug.Assert(null!=obj, "WeakReferenceList.Remove() should not be passed null."); lock(base.SyncRoot) { int index = FindWeakReference(obj); // If the object is not on the list then // we are done. (return false) if(index < 0) return false; return base.RemoveAt(index); } } ////// Insert a weak reference into the List. /// Returns true if successfully inserted. /// Returns false if object is already on the list. /// public bool Insert(int index, object obj) { Debug.Assert(null!=obj, "WeakReferenceList.Add() should not be passed null."); lock(base.SyncRoot) { int existingIndex = FindWeakReference(obj); // If the object is already on the list then // we are done. (return false) if(existingIndex >= 0) return false; return base.Internal_Insert(index, new WeakReference(obj)); } } ////// Find an object on the List. /// Also cleans up dead weakreferences. /// private int FindWeakReference(object obj) { // syncRoot Lock MUST be held by the caller. // Search the LiveList looking for the object, also remove any // dead references.we find. These two operations are combinded // to prevent having to walk the list twice. // // We use the "LiveList" to avoid snapping a Clone everytime we // Change something. // To do this correctly you need to understand how the base class // virtualizes the Copy On Write. ArrayList list = base.LiveList; int foundItem = -1; for(int i = 0; i < list.Count; i++) { WeakReference weakRef = (WeakReference) list[i]; if(weakRef.IsAlive) { if(obj == weakRef.Target) foundItem = i; } else { // Removing dead refs should not invalidate "foundItem" above. // Call the base RemoveAt to preserve the Copy on Write // virtualization. ie. list.RemoveAt(i) would be WRONG! // The RemoveAt call might Clone the LiveList (the first // time only) so we should get a new reference to it. base.RemoveAt(i); list = base.LiveList; // Also the ArrayList will copy-up to fill the Removed element // so back up and do the same index again. i -= 1; } } return foundItem; } } } // 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
- Errors.cs
- CallTemplateAction.cs
- WebPartEditVerb.cs
- AddInProcess.cs
- NumberAction.cs
- NetworkAddressChange.cs
- EtwTrace.cs
- FocusChangedEventArgs.cs
- FileNotFoundException.cs
- IteratorDescriptor.cs
- UserControlCodeDomTreeGenerator.cs
- DbConnectionFactory.cs
- namescope.cs
- sqlstateclientmanager.cs
- QueryContinueDragEventArgs.cs
- ListBoxItem.cs
- GrammarBuilderRuleRef.cs
- CultureSpecificStringDictionary.cs
- IriParsingElement.cs
- AutomationPatternInfo.cs
- IListConverters.cs
- NamespaceListProperty.cs
- Compiler.cs
- DesignerOptionService.cs
- Preprocessor.cs
- StrokeCollectionConverter.cs
- Accessors.cs
- Span.cs
- StandardOleMarshalObject.cs
- SafeFileHandle.cs
- RangeBase.cs
- ManifestSignedXml.cs
- ResXDataNode.cs
- SQLBoolean.cs
- Base64Encoder.cs
- ViewValidator.cs
- InvalidCastException.cs
- XmlSchemaInclude.cs
- DataListCommandEventArgs.cs
- FormsIdentity.cs
- HtmlLink.cs
- WebColorConverter.cs
- AccessDataSource.cs
- DiscreteKeyFrames.cs
- CodeConditionStatement.cs
- FontClient.cs
- Matrix3D.cs
- EncryptedXml.cs
- SqlProcedureAttribute.cs
- JournalEntry.cs
- InteropExecutor.cs
- TableAdapterManagerMethodGenerator.cs
- IgnoreSectionHandler.cs
- SQLInt64.cs
- AbstractDataSvcMapFileLoader.cs
- URLMembershipCondition.cs
- RewritingProcessor.cs
- StubHelpers.cs
- ConstructorArgumentAttribute.cs
- OleDbFactory.cs
- EndOfStreamException.cs
- ContainerAction.cs
- MenuItemCollection.cs
- QueueProcessor.cs
- CredentialCache.cs
- Animatable.cs
- DescendantBaseQuery.cs
- _Win32.cs
- xml.cs
- EntityDesignerBuildProvider.cs
- ScrollChrome.cs
- DataBoundControl.cs
- UnitySerializationHolder.cs
- PerformanceCounterCategory.cs
- KeyGesture.cs
- _NegoStream.cs
- ListViewItemSelectionChangedEvent.cs
- IInstanceTable.cs
- MediaScriptCommandRoutedEventArgs.cs
- DynamicMetaObjectBinder.cs
- MobileResource.cs
- hresults.cs
- HeaderCollection.cs
- Tile.cs
- TemplateControlParser.cs
- XmlAttributes.cs
- RawStylusInput.cs
- metadatamappinghashervisitor.cs
- CanonicalXml.cs
- EncryptedReference.cs
- TimeSpan.cs
- SoapExtension.cs
- FloaterBaseParaClient.cs
- GACMembershipCondition.cs
- GlyphRunDrawing.cs
- NavigationEventArgs.cs
- RegexRunnerFactory.cs
- DPCustomTypeDescriptor.cs
- Pair.cs
- ReachPageContentSerializerAsync.cs