Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Shared / MS / Internal / AppDomainShutdownMonitor.cs / 1 / AppDomainShutdownMonitor.cs
//---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // Implement IAddDomainShutdownListener and use AppDomainShutdownMonitor // to know when the AppDomain is going down // //--------------------------------------------------------------------------- using System; using System.Diagnostics; // Assert using System.Collections.Generic; // Dictionary using System.Threading; // [ThreadStatic] namespace MS.Internal { internal interface IAppDomainShutdownListener { void NotifyShutdown(); } internal static class AppDomainShutdownMonitor { static AppDomainShutdownMonitor() { AppDomain.CurrentDomain.DomainUnload += OnShutdown; AppDomain.CurrentDomain.ProcessExit += OnShutdown; _dictionary = new Dictionary(); } public static void Add(WeakReference listener) { Debug.Assert(listener.Target != null); Debug.Assert(listener.Target is IAppDomainShutdownListener); lock (_dictionary) { if (!_shuttingDown) { _dictionary.Add(listener, listener); } } } public static void Remove(WeakReference listener) { Debug.Assert(listener.Target == null || listener.Target is IAppDomainShutdownListener); lock (_dictionary) { if (!_shuttingDown) { _dictionary.Remove(listener); } } } private static void OnShutdown(object sender, EventArgs e) { lock (_dictionary) { // Setting this to true prevents Add and Remove from modifying the list. This // way we call out without holding a lock (which would be bad) _shuttingDown = true; } foreach (WeakReference value in _dictionary.Values) { IAppDomainShutdownListener listener = value.Target as IAppDomainShutdownListener; if (listener != null) { listener.NotifyShutdown(); } } } private static Dictionary _dictionary; private static bool _shuttingDown; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // // // Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: // Implement IAddDomainShutdownListener and use AppDomainShutdownMonitor // to know when the AppDomain is going down // //--------------------------------------------------------------------------- using System; using System.Diagnostics; // Assert using System.Collections.Generic; // Dictionary using System.Threading; // [ThreadStatic] namespace MS.Internal { internal interface IAppDomainShutdownListener { void NotifyShutdown(); } internal static class AppDomainShutdownMonitor { static AppDomainShutdownMonitor() { AppDomain.CurrentDomain.DomainUnload += OnShutdown; AppDomain.CurrentDomain.ProcessExit += OnShutdown; _dictionary = new Dictionary(); } public static void Add(WeakReference listener) { Debug.Assert(listener.Target != null); Debug.Assert(listener.Target is IAppDomainShutdownListener); lock (_dictionary) { if (!_shuttingDown) { _dictionary.Add(listener, listener); } } } public static void Remove(WeakReference listener) { Debug.Assert(listener.Target == null || listener.Target is IAppDomainShutdownListener); lock (_dictionary) { if (!_shuttingDown) { _dictionary.Remove(listener); } } } private static void OnShutdown(object sender, EventArgs e) { lock (_dictionary) { // Setting this to true prevents Add and Remove from modifying the list. This // way we call out without holding a lock (which would be bad) _shuttingDown = true; } foreach (WeakReference value in _dictionary.Values) { IAppDomainShutdownListener listener = value.Target as IAppDomainShutdownListener; if (listener != null) { listener.NotifyShutdown(); } } } private static Dictionary _dictionary; private static bool _shuttingDown; } } // 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
- FixedSOMPageElement.cs
- AssemblyBuilder.cs
- SafeHandle.cs
- FilteredDataSetHelper.cs
- RadialGradientBrush.cs
- Roles.cs
- SerialPort.cs
- Pair.cs
- MinMaxParagraphWidth.cs
- TemplateInstanceAttribute.cs
- IndexOutOfRangeException.cs
- SizeFConverter.cs
- ClockGroup.cs
- QilInvoke.cs
- Int32Animation.cs
- AppearanceEditorPart.cs
- CellNormalizer.cs
- Geometry3D.cs
- BuildDependencySet.cs
- StaticSiteMapProvider.cs
- Panel.cs
- FamilyTypefaceCollection.cs
- TypeDelegator.cs
- FixedSOMLineCollection.cs
- MetadataItemSerializer.cs
- SoapDocumentMethodAttribute.cs
- DodSequenceMerge.cs
- WorkflowDebuggerSteppingAttribute.cs
- GZipDecoder.cs
- JoinCqlBlock.cs
- EventlogProvider.cs
- XmlAttribute.cs
- DataReaderContainer.cs
- PermissionSet.cs
- PenContext.cs
- WebBrowserContainer.cs
- DBBindings.cs
- PopupControlService.cs
- ZoneMembershipCondition.cs
- XmlValidatingReader.cs
- KeyConstraint.cs
- FrameworkContentElementAutomationPeer.cs
- TextServicesCompartmentContext.cs
- TypedTableBase.cs
- TableItemProviderWrapper.cs
- FeatureSupport.cs
- MultiDataTrigger.cs
- DateRangeEvent.cs
- AuthenticationModulesSection.cs
- LeafCellTreeNode.cs
- TableCellAutomationPeer.cs
- _ListenerResponseStream.cs
- Assembly.cs
- EventBuilder.cs
- ReachPageContentCollectionSerializer.cs
- CommonBehaviorsSection.cs
- MetafileHeader.cs
- CannotUnloadAppDomainException.cs
- ScrollChrome.cs
- InvalidOleVariantTypeException.cs
- UIElementIsland.cs
- QilInvokeLateBound.cs
- BamlResourceDeserializer.cs
- AncillaryOps.cs
- SortedList.cs
- WindowsScrollBarBits.cs
- Int32Rect.cs
- BitmapEffect.cs
- RuntimeResourceSet.cs
- ScrollableControl.cs
- sortedlist.cs
- SendActivity.cs
- PartialTrustVisibleAssemblyCollection.cs
- NativeMethods.cs
- ModifierKeysValueSerializer.cs
- SqlDataSourceCommandEventArgs.cs
- EntityException.cs
- PreviousTrackingServiceAttribute.cs
- EnumValAlphaComparer.cs
- SendKeys.cs
- WebPartsPersonalization.cs
- XmlAnyElementAttribute.cs
- ListViewItem.cs
- SqlProcedureAttribute.cs
- BitmapEffectInputData.cs
- ServiceDescription.cs
- AmbientProperties.cs
- DynamicRendererThreadManager.cs
- DataList.cs
- IndexedString.cs
- DashStyle.cs
- DataTableMappingCollection.cs
- PerformanceCounterManager.cs
- ConfigurationManagerHelperFactory.cs
- Int32Rect.cs
- CharacterString.cs
- DoubleLink.cs
- BinaryNode.cs
- WmfPlaceableFileHeader.cs
- odbcmetadatacollectionnames.cs