Code:
/ 4.0 / 4.0 / untmp / 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DesignTimeVisibleAttribute.cs
- X509Certificate.cs
- CreateUserWizard.cs
- StandardTransformFactory.cs
- AudienceUriMode.cs
- SessionStateUtil.cs
- ImageBrush.cs
- TextTreeTextElementNode.cs
- Menu.cs
- XsdDateTime.cs
- DriveInfo.cs
- ProviderCommandInfoUtils.cs
- BoundPropertyEntry.cs
- SmiRecordBuffer.cs
- ResourceIDHelper.cs
- Page.cs
- WsdlInspector.cs
- WindowsSecurityToken.cs
- HttpStreamXmlDictionaryReader.cs
- activationcontext.cs
- CodeRemoveEventStatement.cs
- PasswordRecoveryAutoFormat.cs
- DeadCharTextComposition.cs
- Processor.cs
- ISAPIApplicationHost.cs
- EdmSchemaError.cs
- InheritedPropertyChangedEventArgs.cs
- DataService.cs
- TrustManagerMoreInformation.cs
- ScrollableControl.cs
- PtsContext.cs
- XmlWriterTraceListener.cs
- DefaultValueConverter.cs
- PassportAuthenticationEventArgs.cs
- ClientScriptItem.cs
- UIAgentMonitor.cs
- TableCellCollection.cs
- FileSecurity.cs
- ResourceContainer.cs
- ListView.cs
- SuppressMessageAttribute.cs
- SessionParameter.cs
- SynthesizerStateChangedEventArgs.cs
- PrintSystemException.cs
- NullReferenceException.cs
- ApplicationException.cs
- Span.cs
- IgnoreDataMemberAttribute.cs
- WebScriptEndpointElement.cs
- StdValidatorsAndConverters.cs
- DataContractSerializerOperationBehavior.cs
- ClientFormsIdentity.cs
- Executor.cs
- XmlSchemaException.cs
- OneWayChannelFactory.cs
- RepeaterCommandEventArgs.cs
- BindingExpressionUncommonField.cs
- EntityStoreSchemaGenerator.cs
- RequestChannel.cs
- StateChangeEvent.cs
- SafeBitVector32.cs
- UrlAuthFailedErrorFormatter.cs
- EventSourceCreationData.cs
- HtmlTableCellCollection.cs
- StandardCommands.cs
- OuterGlowBitmapEffect.cs
- ByteStream.cs
- GlobalProxySelection.cs
- ParameterCollection.cs
- ValidationErrorEventArgs.cs
- ExceptionHandlersDesigner.cs
- ellipse.cs
- FormViewModeEventArgs.cs
- SecurityCriticalDataForSet.cs
- DataGridColumnStyleMappingNameEditor.cs
- ConfigurationConverterBase.cs
- mediaeventshelper.cs
- NullableConverter.cs
- SynchronizedInputProviderWrapper.cs
- Application.cs
- XmlSerializationGeneratedCode.cs
- ScriptingAuthenticationServiceSection.cs
- VisualStyleRenderer.cs
- ProtocolsSection.cs
- ClientScriptItem.cs
- WebEvents.cs
- XmlNamespaceDeclarationsAttribute.cs
- ExpressionBinding.cs
- QilIterator.cs
- Pkcs7Signer.cs
- ZoneButton.cs
- UrlMapping.cs
- FormViewUpdatedEventArgs.cs
- Color.cs
- TextEffectCollection.cs
- PolyQuadraticBezierSegmentFigureLogic.cs
- MenuItemBindingCollection.cs
- HostedTransportConfigurationManager.cs
- CodeAccessSecurityEngine.cs
- XmlSchemaInclude.cs