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
- SelectorAutomationPeer.cs
- ObjRef.cs
- SqlCommandSet.cs
- SurrogateEncoder.cs
- _NativeSSPI.cs
- ImpersonationContext.cs
- TdsParserSessionPool.cs
- CreateDataSourceDialog.cs
- EntityRecordInfo.cs
- CriticalHandle.cs
- NoClickablePointException.cs
- InternalRelationshipCollection.cs
- TypeValidationEventArgs.cs
- Activation.cs
- TextSimpleMarkerProperties.cs
- NavigationProgressEventArgs.cs
- TargetPerspective.cs
- ImageListStreamer.cs
- DataGridViewCellErrorTextNeededEventArgs.cs
- RectangleHotSpot.cs
- Main.cs
- SqlBooleanMismatchVisitor.cs
- SapiGrammar.cs
- CodeBlockBuilder.cs
- Help.cs
- JournalEntryListConverter.cs
- ToolStripPanelCell.cs
- thaishape.cs
- ParameterElement.cs
- TextDecoration.cs
- _SingleItemRequestCache.cs
- AssemblyBuilderData.cs
- CatalogZone.cs
- ParenExpr.cs
- _SecureChannel.cs
- HashHelper.cs
- UserControlCodeDomTreeGenerator.cs
- UserControlBuildProvider.cs
- RegexMatchCollection.cs
- LZCodec.cs
- TimeZone.cs
- DiagnosticTraceSource.cs
- PropertyCondition.cs
- PanelDesigner.cs
- TemplateBindingExtension.cs
- CodeArrayCreateExpression.cs
- Floater.cs
- XmlElementCollection.cs
- ArgumentReference.cs
- Rijndael.cs
- Track.cs
- GridViewRow.cs
- SqlMethodCallConverter.cs
- Image.cs
- Model3DGroup.cs
- DrawToolTipEventArgs.cs
- XmlSchemaAttribute.cs
- ExtenderControl.cs
- Selector.cs
- StateChangeEvent.cs
- IgnoreSectionHandler.cs
- SoapServerMethod.cs
- ChildDocumentBlock.cs
- GifBitmapDecoder.cs
- XmlWriter.cs
- StdValidatorsAndConverters.cs
- ErrorHandlerFaultInfo.cs
- webeventbuffer.cs
- XmlRootAttribute.cs
- AccessibleObject.cs
- TextPointerBase.cs
- DispatcherExceptionFilterEventArgs.cs
- BamlTreeMap.cs
- ToolStripArrowRenderEventArgs.cs
- XmlSchemaExporter.cs
- TdsValueSetter.cs
- XmlSerializerSection.cs
- CharStorage.cs
- XmlComment.cs
- TimeSpanStorage.cs
- DesignOnlyAttribute.cs
- EntryIndex.cs
- EventKeyword.cs
- RightsManagementPermission.cs
- EventLogConfiguration.cs
- DataStreamFromComStream.cs
- SerializableReadOnlyDictionary.cs
- EntityDataSourceColumn.cs
- ModifiableIteratorCollection.cs
- DetailsViewPageEventArgs.cs
- SecurityDescriptor.cs
- AppDomainAttributes.cs
- HwndHostAutomationPeer.cs
- MouseActionValueSerializer.cs
- ClientTarget.cs
- NodeCounter.cs
- SecurityProtocol.cs
- DrawingGroup.cs
- SoapAttributeOverrides.cs
- ThrowHelper.cs