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
- CompiledIdentityConstraint.cs
- KnownColorTable.cs
- HtmlInputPassword.cs
- ObjectTypeMapping.cs
- PriorityQueue.cs
- NegotiationTokenAuthenticatorStateCache.cs
- CookieParameter.cs
- CursorConverter.cs
- WebPartChrome.cs
- OrderByBuilder.cs
- SingleObjectCollection.cs
- SoapReflector.cs
- CuspData.cs
- FormatPage.cs
- Delegate.cs
- CommandSet.cs
- WindowsListBox.cs
- DateTimeUtil.cs
- AppSettingsExpressionEditor.cs
- XamlTypeWithExplicitNamespace.cs
- TextFindEngine.cs
- ModelChangedEventArgsImpl.cs
- PropertyMapper.cs
- Facet.cs
- XPathParser.cs
- SrgsGrammar.cs
- LineProperties.cs
- ImageSource.cs
- DBCSCodePageEncoding.cs
- IisTraceWebEventProvider.cs
- ServicesSection.cs
- FullTrustAssembly.cs
- SharedDp.cs
- EntryIndex.cs
- DiagnosticTrace.cs
- BindingMemberInfo.cs
- ComplexObject.cs
- HasCopySemanticsAttribute.cs
- CaseStatement.cs
- XmlSequenceWriter.cs
- AxHost.cs
- ITreeGenerator.cs
- FillErrorEventArgs.cs
- CustomValidator.cs
- NeedSkipTokenVisitor.cs
- FontInfo.cs
- SQLDecimal.cs
- EditorPartCollection.cs
- DLinqAssociationProvider.cs
- RequestQueue.cs
- NumberSubstitution.cs
- SvcMapFileLoader.cs
- Signature.cs
- TextDocumentView.cs
- ToolStripButton.cs
- TripleDES.cs
- formatter.cs
- SystemIcmpV6Statistics.cs
- TextSearch.cs
- DLinqDataModelProvider.cs
- RectangleGeometry.cs
- TypeExtension.cs
- QueueTransferProtocol.cs
- MessageContractMemberAttribute.cs
- FormViewDeleteEventArgs.cs
- NavigatingCancelEventArgs.cs
- XXXInfos.cs
- Pkcs9Attribute.cs
- UrlMappingCollection.cs
- WebPartConnection.cs
- TCEAdapterGenerator.cs
- FunctionDetailsReader.cs
- Soap.cs
- RoutedEventValueSerializer.cs
- XPathMultyIterator.cs
- SecurityTokenValidationException.cs
- StackSpiller.Temps.cs
- x509store.cs
- TemplatePropertyEntry.cs
- Type.cs
- Pts.cs
- LayoutDump.cs
- DataKeyCollection.cs
- ProfileProvider.cs
- CompositeTypefaceMetrics.cs
- GraphicsState.cs
- GuidTagList.cs
- RepeatButton.cs
- FreezableCollection.cs
- BitmapImage.cs
- RichTextBox.cs
- CellNormalizer.cs
- ITextView.cs
- EventTrigger.cs
- CorrelationManager.cs
- UserCancellationException.cs
- TextEvent.cs
- WebPart.cs
- OuterGlowBitmapEffect.cs
- ChameleonKey.cs