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
- SchemaElementLookUpTable.cs
- Int32CollectionValueSerializer.cs
- ToolStripDropDownClosingEventArgs.cs
- CurrencyWrapper.cs
- SerializationBinder.cs
- sortedlist.cs
- Vector3DCollectionConverter.cs
- FilteredSchemaElementLookUpTable.cs
- SqlDataRecord.cs
- SessionEndingCancelEventArgs.cs
- HandlerMappingMemo.cs
- InvalidProgramException.cs
- Shape.cs
- EventlogProvider.cs
- ScriptResourceHandler.cs
- RichTextBoxConstants.cs
- SqlDataSourceCommandEventArgs.cs
- LayoutTableCell.cs
- Rectangle.cs
- RadioButtonList.cs
- XmlNamespaceMapping.cs
- XmlSerializationGeneratedCode.cs
- TextEditorMouse.cs
- Helpers.cs
- PerformanceCountersElement.cs
- HttpContext.cs
- DynamicRendererThreadManager.cs
- AssemblyAttributesGoHere.cs
- TextServicesContext.cs
- SerializationSectionGroup.cs
- BinaryObjectInfo.cs
- DrawListViewItemEventArgs.cs
- _ChunkParse.cs
- ClrPerspective.cs
- DragStartedEventArgs.cs
- RelationshipFixer.cs
- TextEditorCopyPaste.cs
- EasingFunctionBase.cs
- DesignerSerializationVisibilityAttribute.cs
- StringExpressionSet.cs
- NonDualMessageSecurityOverHttpElement.cs
- WebPartCatalogAddVerb.cs
- AtomMaterializerLog.cs
- exports.cs
- SerializationSectionGroup.cs
- ChtmlFormAdapter.cs
- ElementUtil.cs
- BasicExpressionVisitor.cs
- DesignerEditorPartChrome.cs
- PageContentCollection.cs
- DataKeyArray.cs
- InfocardExtendedInformationCollection.cs
- DefaultValueAttribute.cs
- ActivityIdHeader.cs
- ResourcePart.cs
- ApplicationFileParser.cs
- FilteredXmlReader.cs
- TimelineCollection.cs
- ToolStripContainer.cs
- PerformanceCounterPermission.cs
- DataGridViewTextBoxCell.cs
- Transform3D.cs
- GridViewCommandEventArgs.cs
- InstalledFontCollection.cs
- ToolStripRenderEventArgs.cs
- ResourceCategoryAttribute.cs
- ObjectViewEntityCollectionData.cs
- BindingExpression.cs
- ArrayTypeMismatchException.cs
- ViewLoader.cs
- Action.cs
- XMLSyntaxException.cs
- FilterEventArgs.cs
- TextAdaptor.cs
- CompilerScopeManager.cs
- EventLogEntry.cs
- MsmqAppDomainProtocolHandler.cs
- SqlDataSourceSelectingEventArgs.cs
- StateWorkerRequest.cs
- SerialStream.cs
- XmlIlTypeHelper.cs
- StructuredType.cs
- FacetEnabledSchemaElement.cs
- Translator.cs
- MetadataSource.cs
- CharEnumerator.cs
- DispatcherBuilder.cs
- EventDescriptor.cs
- HtmlInputControl.cs
- Util.cs
- regiisutil.cs
- InvalidWMPVersionException.cs
- AdornerLayer.cs
- TypeSystem.cs
- WorkflowPersistenceService.cs
- QuaternionRotation3D.cs
- SamlAssertion.cs
- Stroke2.cs
- DataGridViewComboBoxEditingControl.cs
- SoapEnvelopeProcessingElement.cs