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
- SafeWaitHandle.cs
- CreateUserWizardDesigner.cs
- XmlJsonReader.cs
- LassoHelper.cs
- SqlCommand.cs
- EditorPart.cs
- StoreAnnotationsMap.cs
- RequestUriProcessor.cs
- PageMediaSize.cs
- SkewTransform.cs
- XmlSignificantWhitespace.cs
- ReadOnlyAttribute.cs
- LinqToSqlWrapper.cs
- DataGridPageChangedEventArgs.cs
- WebPartDisplayMode.cs
- SHA384.cs
- InputProcessorProfiles.cs
- HttpConfigurationSystem.cs
- CompilerLocalReference.cs
- WebScriptEnablingBehavior.cs
- SingleTagSectionHandler.cs
- TypeConverterHelper.cs
- CompilationRelaxations.cs
- RequestCachingSection.cs
- VisualTreeUtils.cs
- DuplicateWaitObjectException.cs
- PolyBezierSegment.cs
- GridViewRow.cs
- WebBrowser.cs
- IdnElement.cs
- Dictionary.cs
- GridViewColumnHeader.cs
- StackBuilderSink.cs
- ReadOnlyDataSource.cs
- SelectionChangedEventArgs.cs
- BeginStoryboard.cs
- SqlGenericUtil.cs
- ThreadStaticAttribute.cs
- WindowsTreeView.cs
- Clause.cs
- WebPartsPersonalization.cs
- XmlHierarchicalEnumerable.cs
- EntitySqlException.cs
- ContractReference.cs
- WebBrowserBase.cs
- UInt32Storage.cs
- MemberExpression.cs
- WebPartConnectVerb.cs
- RoutedEvent.cs
- DataServiceProviderWrapper.cs
- QueryReaderSettings.cs
- DrawingAttributes.cs
- AppendHelper.cs
- CachedTypeface.cs
- TypedDatasetGenerator.cs
- StandardTransformFactory.cs
- OpenFileDialog.cs
- BooleanSwitch.cs
- ShaderRenderModeValidation.cs
- MimeMapping.cs
- StringUtil.cs
- XmlTextWriter.cs
- SharedUtils.cs
- VisualBasicReference.cs
- TableLayout.cs
- FlowDocumentFormatter.cs
- TextElementCollectionHelper.cs
- WindowsToolbarItemAsMenuItem.cs
- PowerModeChangedEventArgs.cs
- baseshape.cs
- Int32Storage.cs
- GlyphInfoList.cs
- StyleHelper.cs
- X509RecipientCertificateServiceElement.cs
- BlockCollection.cs
- Timeline.cs
- DictionarySurrogate.cs
- Pens.cs
- HtmlTitle.cs
- IndexerNameAttribute.cs
- ExecutionContext.cs
- ClickablePoint.cs
- DBBindings.cs
- StandardCommands.cs
- TextRange.cs
- BooleanToVisibilityConverter.cs
- EntityKey.cs
- SwitchLevelAttribute.cs
- GridView.cs
- AnnotationResourceChangedEventArgs.cs
- CommonRemoteMemoryBlock.cs
- IDispatchConstantAttribute.cs
- PropertyConverter.cs
- StyleSelector.cs
- Int16.cs
- SystemWebExtensionsSectionGroup.cs
- ApplicationFileCodeDomTreeGenerator.cs
- XmlReturnReader.cs
- COAUTHINFO.cs
- ObjectDataSourceMethodEventArgs.cs