Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Core / CSharp / System / Windows / Media / UniqueEventHelper.cs / 1 / UniqueEventHelper.cs
using System;
using System.Collections;
using System.Diagnostics;
namespace System.Windows.Media
{
///
/// Aids in making events unique. i.e. you register the same function for
/// the same event twice, but it will only be called once for each time
/// the event is invoked.
///
/// UniqueEventHelper should only be accessed from the UI thread so that
/// handlers that throw exceptions will crash the app, making the developer
/// aware of the problem.
///
internal class UniqueEventHelper
where TEventArgs : EventArgs
{
///
/// Add the handler to the list of handlers associated with this event.
/// If the handler has already been added, we simply increment the ref
/// count. That way if this same handler has already been added, it
/// won't be invoked multiple times when the event is raised.
///
internal void AddEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] == null)
{
_htDelegates.Add(handler, 1);
}
else
{
int refCount = (int)_htDelegates[handler] + 1;
_htDelegates[handler] = refCount;
}
}
///
/// Removed the handler from the list of handlers associated with this
/// event. If the handler has been added multiple times (more times than
/// it has been removed), we simply decrement its ref count.
///
internal void RemoveEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] != null)
{
int refCount = (int)_htDelegates[handler];
if (refCount == 1)
{
_htDelegates.Remove(handler);
}
else
{
_htDelegates[handler] = refCount - 1;
}
}
}
///
/// Enumerates all the keys in the hashtable, which must be EventHandlers,
/// and invokes them.
///
/// The sender for the callback.
/// The args object to be sent by the delegate
internal void InvokeEvents(object sender, TEventArgs args)
{
Debug.Assert((sender != null), "Sender is null");
if (_htDelegates != null)
{
Hashtable htDelegates = (Hashtable)_htDelegates.Clone();
foreach (EventHandler handler in htDelegates.Keys)
{
Debug.Assert((handler != null), "Event handler is null");
handler(sender, args);
}
}
}
///
/// Clones the event helper
///
internal UniqueEventHelper Clone()
{
UniqueEventHelper ueh = new UniqueEventHelper();
if (_htDelegates != null)
{
ueh._htDelegates = (Hashtable)_htDelegates.Clone();
}
return ueh;
}
///
/// Ensures Hashtable is created so that event handlers can be added/removed
///
private void EnsureEventTable()
{
if (_htDelegates == null)
{
_htDelegates = new Hashtable();
}
}
private Hashtable _htDelegates;
}
//
internal class UniqueEventHelper
{
///
/// Add the handler to the list of handlers associated with this event.
/// If the handler has already been added, we simply increment the ref
/// count. That way if this same handler has already been added, it
/// won't be invoked multiple times when the event is raised.
///
internal void AddEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] == null)
{
_htDelegates.Add(handler, 1);
}
else
{
int refCount = (int)_htDelegates[handler] + 1;
_htDelegates[handler] = refCount;
}
}
///
/// Removed the handler from the list of handlers associated with this
/// event. If the handler has been added multiple times (more times than
/// it has been removed), we simply decrement its ref count.
///
internal void RemoveEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] != null)
{
int refCount = (int)_htDelegates[handler];
if (refCount == 1)
{
_htDelegates.Remove(handler);
}
else
{
_htDelegates[handler] = refCount - 1;
}
}
}
///
/// Enumerates all the keys in the hashtable, which must be EventHandlers,
/// and invokes them.
///
/// The sender for the callback.
/// The args object to be sent by the delegate
internal void InvokeEvents(object sender, EventArgs args)
{
Debug.Assert((sender != null), "Sender is null");
if (_htDelegates != null)
{
Hashtable htDelegates = (Hashtable)_htDelegates.Clone();
foreach (EventHandler handler in htDelegates.Keys)
{
Debug.Assert((handler != null), "Event handler is null");
handler(sender, args);
}
}
}
///
/// Clones the event helper
///
internal UniqueEventHelper Clone()
{
UniqueEventHelper ueh = new UniqueEventHelper();
if (_htDelegates != null)
{
ueh._htDelegates = (Hashtable)_htDelegates.Clone();
}
return ueh;
}
///
/// Ensures Hashtable is created so that event handlers can be added/removed
///
private void EnsureEventTable()
{
if (_htDelegates == null)
{
_htDelegates = new Hashtable();
}
}
private Hashtable _htDelegates;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections;
using System.Diagnostics;
namespace System.Windows.Media
{
///
/// Aids in making events unique. i.e. you register the same function for
/// the same event twice, but it will only be called once for each time
/// the event is invoked.
///
/// UniqueEventHelper should only be accessed from the UI thread so that
/// handlers that throw exceptions will crash the app, making the developer
/// aware of the problem.
///
internal class UniqueEventHelper
where TEventArgs : EventArgs
{
///
/// Add the handler to the list of handlers associated with this event.
/// If the handler has already been added, we simply increment the ref
/// count. That way if this same handler has already been added, it
/// won't be invoked multiple times when the event is raised.
///
internal void AddEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] == null)
{
_htDelegates.Add(handler, 1);
}
else
{
int refCount = (int)_htDelegates[handler] + 1;
_htDelegates[handler] = refCount;
}
}
///
/// Removed the handler from the list of handlers associated with this
/// event. If the handler has been added multiple times (more times than
/// it has been removed), we simply decrement its ref count.
///
internal void RemoveEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] != null)
{
int refCount = (int)_htDelegates[handler];
if (refCount == 1)
{
_htDelegates.Remove(handler);
}
else
{
_htDelegates[handler] = refCount - 1;
}
}
}
///
/// Enumerates all the keys in the hashtable, which must be EventHandlers,
/// and invokes them.
///
/// The sender for the callback.
/// The args object to be sent by the delegate
internal void InvokeEvents(object sender, TEventArgs args)
{
Debug.Assert((sender != null), "Sender is null");
if (_htDelegates != null)
{
Hashtable htDelegates = (Hashtable)_htDelegates.Clone();
foreach (EventHandler handler in htDelegates.Keys)
{
Debug.Assert((handler != null), "Event handler is null");
handler(sender, args);
}
}
}
///
/// Clones the event helper
///
internal UniqueEventHelper Clone()
{
UniqueEventHelper ueh = new UniqueEventHelper();
if (_htDelegates != null)
{
ueh._htDelegates = (Hashtable)_htDelegates.Clone();
}
return ueh;
}
///
/// Ensures Hashtable is created so that event handlers can be added/removed
///
private void EnsureEventTable()
{
if (_htDelegates == null)
{
_htDelegates = new Hashtable();
}
}
private Hashtable _htDelegates;
}
//
internal class UniqueEventHelper
{
///
/// Add the handler to the list of handlers associated with this event.
/// If the handler has already been added, we simply increment the ref
/// count. That way if this same handler has already been added, it
/// won't be invoked multiple times when the event is raised.
///
internal void AddEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] == null)
{
_htDelegates.Add(handler, 1);
}
else
{
int refCount = (int)_htDelegates[handler] + 1;
_htDelegates[handler] = refCount;
}
}
///
/// Removed the handler from the list of handlers associated with this
/// event. If the handler has been added multiple times (more times than
/// it has been removed), we simply decrement its ref count.
///
internal void RemoveEvent(EventHandler handler)
{
if (handler == null)
{
throw new System.ArgumentNullException("handler");
}
EnsureEventTable();
if (_htDelegates[handler] != null)
{
int refCount = (int)_htDelegates[handler];
if (refCount == 1)
{
_htDelegates.Remove(handler);
}
else
{
_htDelegates[handler] = refCount - 1;
}
}
}
///
/// Enumerates all the keys in the hashtable, which must be EventHandlers,
/// and invokes them.
///
/// The sender for the callback.
/// The args object to be sent by the delegate
internal void InvokeEvents(object sender, EventArgs args)
{
Debug.Assert((sender != null), "Sender is null");
if (_htDelegates != null)
{
Hashtable htDelegates = (Hashtable)_htDelegates.Clone();
foreach (EventHandler handler in htDelegates.Keys)
{
Debug.Assert((handler != null), "Event handler is null");
handler(sender, args);
}
}
}
///
/// Clones the event helper
///
internal UniqueEventHelper Clone()
{
UniqueEventHelper ueh = new UniqueEventHelper();
if (_htDelegates != null)
{
ueh._htDelegates = (Hashtable)_htDelegates.Clone();
}
return ueh;
}
///
/// Ensures Hashtable is created so that event handlers can be added/removed
///
private void EnsureEventTable()
{
if (_htDelegates == null)
{
_htDelegates = new Hashtable();
}
}
private Hashtable _htDelegates;
}
}
// 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
- VisualStyleInformation.cs
- ProtectedProviderSettings.cs
- UpDownBase.cs
- ImageButton.cs
- WindowsToolbarItemAsMenuItem.cs
- BehaviorService.cs
- ListItemViewControl.cs
- ComponentConverter.cs
- CorruptingExceptionCommon.cs
- IPEndPoint.cs
- TextViewElement.cs
- RelationshipDetailsCollection.cs
- HtmlEmptyTagControlBuilder.cs
- SoapRpcServiceAttribute.cs
- ControlParameter.cs
- ApplicationSettingsBase.cs
- isolationinterop.cs
- PathFigureCollectionValueSerializer.cs
- PersonalizableAttribute.cs
- DetailsView.cs
- __Error.cs
- WebScriptMetadataInstanceContextProvider.cs
- MetadataPropertyCollection.cs
- BasePattern.cs
- DataGridCommandEventArgs.cs
- CqlErrorHelper.cs
- XNodeSchemaApplier.cs
- LinqDataSourceUpdateEventArgs.cs
- FixUpCollection.cs
- UniqueEventHelper.cs
- TTSEngineProxy.cs
- CompressStream.cs
- ClientConfigurationHost.cs
- ClientScriptManagerWrapper.cs
- CustomErrorsSectionWrapper.cs
- shaper.cs
- AndAlso.cs
- StyleSelector.cs
- PerformanceCounterManager.cs
- RangeContentEnumerator.cs
- ParserExtension.cs
- RoutedCommand.cs
- BinaryObjectReader.cs
- EncryptedData.cs
- CuspData.cs
- AnchoredBlock.cs
- ControlBindingsCollection.cs
- BlockExpression.cs
- PathNode.cs
- RelationshipNavigation.cs
- X509CertificateCollection.cs
- TreeViewImageIndexConverter.cs
- XmlSchemaImport.cs
- DecoderReplacementFallback.cs
- ZipIOFileItemStream.cs
- ToolBarButton.cs
- StaticResourceExtension.cs
- Int32CAMarshaler.cs
- ReadOnlyDictionary.cs
- SafeCancelMibChangeNotify.cs
- XPathSelectionIterator.cs
- IntegrationExceptionEventArgs.cs
- RC2.cs
- HashAlgorithm.cs
- GlyphRunDrawing.cs
- LinqDataSourceValidationException.cs
- SendActivity.cs
- SqlUtils.cs
- XmlName.cs
- ColumnMapTranslator.cs
- AppDomainManager.cs
- BaseComponentEditor.cs
- PersianCalendar.cs
- CLSCompliantAttribute.cs
- UrlMappingCollection.cs
- SamlSecurityTokenAuthenticator.cs
- ResourceReader.cs
- LogicalExpr.cs
- ZeroOpNode.cs
- Setter.cs
- PlainXmlWriter.cs
- ProxyOperationRuntime.cs
- SendMailErrorEventArgs.cs
- SecurityTokenResolver.cs
- KnownBoxes.cs
- WebPartDisplayModeCancelEventArgs.cs
- _Rfc2616CacheValidators.cs
- TrackBarRenderer.cs
- XmlEncodedRawTextWriter.cs
- StickyNoteContentControl.cs
- LookupBindingPropertiesAttribute.cs
- PropertyCollection.cs
- DocumentScope.cs
- MediaElement.cs
- BooleanExpr.cs
- Point3DCollectionConverter.cs
- ParseElement.cs
- CodeCatchClauseCollection.cs
- OdbcParameterCollection.cs
- CommandID.cs