Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / 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
- TemplatedMailWebEventProvider.cs
- RadioButtonAutomationPeer.cs
- PassportPrincipal.cs
- GeneralTransform2DTo3DTo2D.cs
- MinMaxParagraphWidth.cs
- ConfigurationManagerInternalFactory.cs
- Int16Storage.cs
- DocumentXmlWriter.cs
- TemplateXamlTreeBuilder.cs
- LayoutManager.cs
- dtdvalidator.cs
- VideoDrawing.cs
- CalendarDataBindingHandler.cs
- StopRoutingHandler.cs
- ThreadInterruptedException.cs
- WebPartConnectVerb.cs
- WindowsListViewScroll.cs
- ColumnHeaderConverter.cs
- WindowsFormsSynchronizationContext.cs
- UpdateTranslator.cs
- ParameterBuilder.cs
- StoreContentChangedEventArgs.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- CountdownEvent.cs
- NativeRightsManagementAPIsStructures.cs
- DataRowExtensions.cs
- TextEditorContextMenu.cs
- DiscoveryUtility.cs
- ParameterCollection.cs
- DataGridViewCellConverter.cs
- FullTrustAssembly.cs
- ExpressionSelection.cs
- TimestampInformation.cs
- FixedPageStructure.cs
- EpmContentDeSerializer.cs
- ConfigXmlElement.cs
- SqlInternalConnection.cs
- ContextBase.cs
- SymbolDocumentInfo.cs
- SQLMembershipProvider.cs
- EventHandlersDesigner.cs
- EditorServiceContext.cs
- ScrollItemPattern.cs
- DataGridViewCellCancelEventArgs.cs
- storepermission.cs
- CachedPathData.cs
- ResourceDescriptionAttribute.cs
- TdsParserSafeHandles.cs
- ReturnType.cs
- DataReaderContainer.cs
- CompressedStack.cs
- RecordsAffectedEventArgs.cs
- LabelInfo.cs
- NodeCounter.cs
- FtpWebResponse.cs
- Speller.cs
- ConnectionManagementElement.cs
- Utility.cs
- SymmetricCryptoHandle.cs
- StackSpiller.Bindings.cs
- BamlBinaryWriter.cs
- SendMailErrorEventArgs.cs
- PolyBezierSegment.cs
- AddInActivator.cs
- ObjectSpanRewriter.cs
- ForeignKeyFactory.cs
- assertwrapper.cs
- _SpnDictionary.cs
- WebServiceReceive.cs
- BitSet.cs
- SqlDataSourceCustomCommandEditor.cs
- ThreadStaticAttribute.cs
- XmlObjectSerializerContext.cs
- ScriptBehaviorDescriptor.cs
- HttpProfileGroupBase.cs
- FlowLayoutPanel.cs
- BaseValidator.cs
- AsymmetricSignatureDeformatter.cs
- Calendar.cs
- RenderCapability.cs
- SqlUdtInfo.cs
- ETagAttribute.cs
- HMACSHA512.cs
- WindowsAuthenticationEventArgs.cs
- ExceptionRoutedEventArgs.cs
- ToolStripControlHost.cs
- DefaultValueTypeConverter.cs
- SourceFileBuildProvider.cs
- CodeTryCatchFinallyStatement.cs
- GifBitmapDecoder.cs
- HideDisabledControlAdapter.cs
- _PooledStream.cs
- Keywords.cs
- Permission.cs
- DecimalStorage.cs
- PolyLineSegmentFigureLogic.cs
- DataServiceQueryException.cs
- SoapAttributeAttribute.cs
- SoundPlayerAction.cs
- RoleService.cs