Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / 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
- SQLBinaryStorage.cs
- XmlSchemaNotation.cs
- GenericXmlSecurityToken.cs
- MouseButtonEventArgs.cs
- WmlPageAdapter.cs
- SystemColors.cs
- LeafCellTreeNode.cs
- PermissionSetEnumerator.cs
- StateDesigner.cs
- X509ChainPolicy.cs
- RealizationDrawingContextWalker.cs
- RegexStringValidator.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- CustomAttributeSerializer.cs
- NamespaceEmitter.cs
- XmlRootAttribute.cs
- DebugController.cs
- WinFormsUtils.cs
- UnwrappedTypesXmlSerializerManager.cs
- ColorAnimationBase.cs
- ParseHttpDate.cs
- SimpleType.cs
- ContextMenu.cs
- RandomDelaySendsAsyncResult.cs
- Point3DValueSerializer.cs
- SchemaNamespaceManager.cs
- LambdaCompiler.Unary.cs
- CellIdBoolean.cs
- Formatter.cs
- CaseInsensitiveHashCodeProvider.cs
- ChannelManager.cs
- XmlSchemaComplexContentExtension.cs
- TimeBoundedCache.cs
- SafeSerializationManager.cs
- RelationshipConstraintValidator.cs
- PageRanges.cs
- DataBindingCollectionEditor.cs
- ECDiffieHellmanCng.cs
- WebServiceData.cs
- LoadedOrUnloadedOperation.cs
- ThreadAbortException.cs
- BookmarkUndoUnit.cs
- DBSqlParserTable.cs
- RequestSecurityTokenResponseCollection.cs
- KeyedHashAlgorithm.cs
- AsnEncodedData.cs
- SpAudioStreamWrapper.cs
- Debug.cs
- DummyDataSource.cs
- AssertHelper.cs
- ConfigurationValues.cs
- TypeConverterValueSerializer.cs
- Clause.cs
- DesignUtil.cs
- RegisteredDisposeScript.cs
- SoapElementAttribute.cs
- Matrix.cs
- ClientBuildManagerCallback.cs
- WindowsSpinner.cs
- WindowPatternIdentifiers.cs
- DebugView.cs
- SemanticResultKey.cs
- PublisherMembershipCondition.cs
- GeneralTransform2DTo3D.cs
- SignatureResourcePool.cs
- SafeRegistryHandle.cs
- FontSizeConverter.cs
- StylusShape.cs
- FixedSOMTable.cs
- WsdlBuildProvider.cs
- Semaphore.cs
- FolderBrowserDialogDesigner.cs
- ValueChangedEventManager.cs
- UIElement.cs
- XmlBoundElement.cs
- Wizard.cs
- HtmlAnchor.cs
- DependencyObjectPropertyDescriptor.cs
- DataGridViewSelectedRowCollection.cs
- ImageAnimator.cs
- HWStack.cs
- StatusBar.cs
- LogWriteRestartAreaState.cs
- DrawingGroupDrawingContext.cs
- SqlCacheDependency.cs
- XPathDocumentIterator.cs
- AtomContentProperty.cs
- NativeMethods.cs
- XmlEnumAttribute.cs
- TableDesigner.cs
- IndexingContentUnit.cs
- SafeNativeMethods.cs
- SessionStateUtil.cs
- TreeIterator.cs
- SafeCancelMibChangeNotify.cs
- PasswordBox.cs
- SelectionEditor.cs
- PropertiesTab.cs
- XmlCharacterData.cs
- TreeViewImageKeyConverter.cs