Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Shared / MS / Internal / AppDomainShutdownMonitor.cs / 1305600 / 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
- CodeCommentStatement.cs
- FormsAuthenticationUser.cs
- GrammarBuilderPhrase.cs
- EncryptedPackage.cs
- OracleConnectionString.cs
- WindowsListViewGroup.cs
- MasterPageBuildProvider.cs
- InfoCardX509Validator.cs
- RouteParametersHelper.cs
- BulletChrome.cs
- AudioFormatConverter.cs
- ThicknessAnimationUsingKeyFrames.cs
- EncryptedHeaderXml.cs
- PolicyLevel.cs
- XomlCompilerHelpers.cs
- SafeCoTaskMem.cs
- StylusPointProperty.cs
- SafeSecurityHelper.cs
- MouseCaptureWithinProperty.cs
- WorkflowControlEndpoint.cs
- TextSegment.cs
- PeerTransportBindingElement.cs
- ValidationResult.cs
- AbstractSvcMapFileLoader.cs
- HttpAsyncResult.cs
- Panel.cs
- _DisconnectOverlappedAsyncResult.cs
- Publisher.cs
- KeyedCollection.cs
- UnsafeCollabNativeMethods.cs
- DataGridViewHitTestInfo.cs
- ClientSession.cs
- DataGridColumnCollection.cs
- XhtmlBasicTextViewAdapter.cs
- GridViewUpdateEventArgs.cs
- XmlSerializerVersionAttribute.cs
- CounterSampleCalculator.cs
- PrimitiveRenderer.cs
- ValidationHelper.cs
- externdll.cs
- HttpCacheVary.cs
- RuntimeTransactionHandle.cs
- Stacktrace.cs
- XPathNodeList.cs
- documentsequencetextview.cs
- StylusPointCollection.cs
- WebRequestModulesSection.cs
- AnnotationService.cs
- VirtualizedItemPattern.cs
- SplitterPanel.cs
- DocumentViewer.cs
- ToolZone.cs
- WebBrowserProgressChangedEventHandler.cs
- IfJoinedCondition.cs
- QilNode.cs
- LinqDataSourceSelectEventArgs.cs
- CallContext.cs
- WebAdminConfigurationHelper.cs
- ObjectHandle.cs
- ToolStripLocationCancelEventArgs.cs
- XmlArrayItemAttributes.cs
- Section.cs
- TemplateBaseAction.cs
- TypeValidationEventArgs.cs
- MultiViewDesigner.cs
- TreeNodeStyle.cs
- LicenseProviderAttribute.cs
- PageRanges.cs
- TextBox.cs
- PreviewPrintController.cs
- WindowsGraphics.cs
- TabControlEvent.cs
- InstanceCreationEditor.cs
- BindingGroup.cs
- ContentPosition.cs
- QueryInterceptorAttribute.cs
- SqlNotificationEventArgs.cs
- UInt32.cs
- WebPartDescription.cs
- HttpCapabilitiesBase.cs
- XPathSingletonIterator.cs
- ObjectManager.cs
- SurrogateDataContract.cs
- WindowsIPAddress.cs
- DataGridViewRowConverter.cs
- Token.cs
- DateTimeAutomationPeer.cs
- FixedTextView.cs
- DataObject.cs
- ExcCanonicalXml.cs
- DesignerTextBoxAdapter.cs
- NeutralResourcesLanguageAttribute.cs
- EffectiveValueEntry.cs
- UnsafeNativeMethods.cs
- DataGridDefaultColumnWidthTypeConverter.cs
- JobInputBins.cs
- SqlTypeSystemProvider.cs
- EnumValAlphaComparer.cs
- Highlights.cs
- UnlockInstanceCommand.cs