Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Data / System / Data / DataViewListener.cs / 2 / DataViewListener.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //[....] //----------------------------------------------------------------------------- namespace System.Data { using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; internal sealed class DataViewListener { private readonly WeakReference _dvWeak; private DataTable _table; private Index _index; ///internal readonly int ObjectID; internal DataViewListener(DataView dv) { this.ObjectID = dv.ObjectID; _dvWeak = new WeakReference(dv); } private void ChildRelationCollectionChanged(object sender, CollectionChangeEventArgs e) { DataView dv = (DataView)_dvWeak.Target; if (dv != null) { dv.ChildRelationCollectionChanged(sender, e); } else { CleanUp(true); } } private void ParentRelationCollectionChanged(object sender, CollectionChangeEventArgs e) { DataView dv = (DataView)_dvWeak.Target; if (dv != null) { dv.ParentRelationCollectionChanged(sender, e); } else { CleanUp(true); } } private void ColumnCollectionChanged(object sender, CollectionChangeEventArgs e) { DataView dv = (DataView)_dvWeak.Target; if (dv != null) { dv.ColumnCollectionChangedInternal(sender, e); } else { CleanUp(true); } } /// /// Maintain the DataView before internal void MaintainDataView(ListChangedType changedType, DataRow row, bool trackAddRemove) { DataView dv = (DataView)_dvWeak.Target; if (dv != null) { dv.MaintainDataView(changedType, row, trackAddRemove); } else { CleanUp(true); } } internal void IndexListChanged(ListChangedEventArgs e) { DataView dv = (DataView)_dvWeak.Target; if (dv != null) { dv.IndexListChangedInternal(e); } else { CleanUp(true); } } internal void RegisterMetaDataEvents(DataTable table) { Debug.Assert(null == _table, "DataViewListener already registered table"); _table = table; if (table != null) { // actively remove listeners without a target RegisterListener(table); // start listening to events CollectionChangeEventHandler handlerCollection = new CollectionChangeEventHandler(ColumnCollectionChanged); table.Columns.ColumnPropertyChanged += handlerCollection; table.Columns.CollectionChanged += handlerCollection; CollectionChangeEventHandler handlerChildRelation = new CollectionChangeEventHandler(ChildRelationCollectionChanged); ((DataRelationCollection.DataTableRelationCollection)(table.ChildRelations)).RelationPropertyChanged += handlerChildRelation; table.ChildRelations.CollectionChanged += handlerChildRelation; CollectionChangeEventHandler handlerParentRelation = new CollectionChangeEventHandler(ParentRelationCollectionChanged); ((DataRelationCollection.DataTableRelationCollection)(table.ParentRelations)).RelationPropertyChanged += handlerParentRelation; table.ParentRelations.CollectionChanged += handlerParentRelation; } } internal void UnregisterMetaDataEvents() { UnregisterMetaDataEvents(true); } private void UnregisterMetaDataEvents(bool updateListeners) { DataTable table = _table; _table = null; if (table != null) { CollectionChangeEventHandler handlerCollection = new CollectionChangeEventHandler(ColumnCollectionChanged); table.Columns.ColumnPropertyChanged -= handlerCollection; table.Columns.CollectionChanged -= handlerCollection; CollectionChangeEventHandler handlerChildRelation = new CollectionChangeEventHandler(ChildRelationCollectionChanged); ((DataRelationCollection.DataTableRelationCollection)(table.ChildRelations)).RelationPropertyChanged -= handlerChildRelation; table.ChildRelations.CollectionChanged -= handlerChildRelation; CollectionChangeEventHandler handlerParentRelation = new CollectionChangeEventHandler(ParentRelationCollectionChanged); ((DataRelationCollection.DataTableRelationCollection)(table.ParentRelations)).RelationPropertyChanged -= handlerParentRelation; table.ParentRelations.CollectionChanged -= handlerParentRelation; if (updateListeners) { Listis raised. /// listeners = table.GetListeners(); lock (listeners) { listeners.Remove(this); } } } } internal void RegisterListChangedEvent(Index index) { Debug.Assert(null == _index, "DataviewListener already registered index"); _index = index; if (null != index) { lock (index) { index.AddRef(); index.ListChangedAdd(this); } } } internal void UnregisterListChangedEvent() { Index index = _index; _index = null; if (index != null) { lock (index) { index.ListChangedRemove(this); if (index.RemoveRef() <= 1) { index.RemoveRef(); } } } } private void CleanUp(bool updateListeners) { UnregisterMetaDataEvents(updateListeners); UnregisterListChangedEvent(); } private void RegisterListener(DataTable table) { List listeners = table.GetListeners(); lock (listeners) { for (int i = listeners.Count - 1; 0 <= i; --i) { DataViewListener listener = listeners[i]; if (!listener._dvWeak.IsAlive) { listeners.RemoveAt(i); listener.CleanUp(false); } } listeners.Add(this); } } } } // 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
- SHA1.cs
- ToolStripLabel.cs
- CompositeTypefaceMetrics.cs
- OleDbRowUpdatedEvent.cs
- ExpressionEditorSheet.cs
- CqlBlock.cs
- CodeArgumentReferenceExpression.cs
- CustomCredentialPolicy.cs
- AutomationEvent.cs
- ClientSponsor.cs
- ListViewGroupConverter.cs
- SqlReferenceCollection.cs
- SerialReceived.cs
- DataGridColumn.cs
- MSG.cs
- StringToken.cs
- HwndSourceKeyboardInputSite.cs
- infer.cs
- SqlDuplicator.cs
- ModulesEntry.cs
- CharConverter.cs
- ExpandCollapseProviderWrapper.cs
- CloudCollection.cs
- SettingsPropertyWrongTypeException.cs
- DiscoveryDocumentSearchPattern.cs
- UnaryExpressionHelper.cs
- XmlUrlResolver.cs
- ScriptControlDescriptor.cs
- FontWeightConverter.cs
- InfoCardArgumentException.cs
- DesignerRegionCollection.cs
- InputQueue.cs
- COM2Enum.cs
- ConfigXmlElement.cs
- DrawingCollection.cs
- UnsafeNativeMethods.cs
- StylusPointDescription.cs
- ToolStripManager.cs
- TypeUtil.cs
- WebPartConnection.cs
- GeometryDrawing.cs
- StrongNameUtility.cs
- WebPartTransformerCollection.cs
- TypeConverterHelper.cs
- TrackingServices.cs
- ControlBindingsCollection.cs
- EUCJPEncoding.cs
- DesignerActionList.cs
- CheckBoxFlatAdapter.cs
- BaseValidator.cs
- SqlUserDefinedAggregateAttribute.cs
- ProcessManager.cs
- TriggerAction.cs
- HttpDigestClientElement.cs
- OracleFactory.cs
- SectionRecord.cs
- XsdBuildProvider.cs
- HtmlContainerControl.cs
- DataBindingList.cs
- AttributeProviderAttribute.cs
- SqlFunctionAttribute.cs
- QueueProcessor.cs
- SqlDataSourceCommandEventArgs.cs
- ControllableStoryboardAction.cs
- MenuRendererStandards.cs
- TdsRecordBufferSetter.cs
- BindingCollection.cs
- ScriptReferenceEventArgs.cs
- DeferredElementTreeState.cs
- dtdvalidator.cs
- ListViewSortEventArgs.cs
- UnsafeNativeMethodsCLR.cs
- PerformanceCounter.cs
- DataStreams.cs
- BitmapDownload.cs
- DetailsViewCommandEventArgs.cs
- Literal.cs
- BoundColumn.cs
- HitTestResult.cs
- GridViewCommandEventArgs.cs
- MetadataUtil.cs
- DrawingVisual.cs
- storepermission.cs
- PolyLineSegmentFigureLogic.cs
- DataGridViewDesigner.cs
- DrawingBrush.cs
- AutomationAttributeInfo.cs
- MenuCommandsChangedEventArgs.cs
- AttachedAnnotationChangedEventArgs.cs
- ConfigurationLocation.cs
- Debug.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- XmlCharType.cs
- Automation.cs
- ExpandCollapsePattern.cs
- PrimitiveDataContract.cs
- ConstructorNeedsTagAttribute.cs
- ToolboxComponentsCreatingEventArgs.cs
- PlacementWorkspace.cs
- ColorInterpolationModeValidation.cs