Click here to Skip to main content
15,889,096 members
Articles / Programming Languages / C#

Tweaked Events

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
18 Dec 2010MIT18 min read 34.8K   289   41  
Framework for customizing events. Comes with Weak Events and Synced Events
using System;
using System.ComponentModel;
using JpLabs.TweakedEvents.Synced;
using System.Threading;
using System.Windows.Threading;

namespace JpLabs.TweakedEvents
{
	public sealed class SyncedEvent<TEH> : TweakedEvent<TEH> where TEH : class
	{
		// Members

		/// <summary>
		/// Supported types are <code>ISynchronizeInvoke</code> and <code>SynchronizationContext</code>
		/// </summary>
		public object SyncronizerObject { get; set; }

		public bool RestrictEventEntryType { get; set; }
		
		// Contructors
		public SyncedEvent() : base ()
		{}
		
		private SyncedEvent(SyncedEvent<TEH> other, IEventEntry<TEH>[] entries) : base(entries) //IList<IEventEntry<TEH>> entries) : base(entries)
		{
			this.SyncronizerObject = other.SyncronizerObject;
			this.RestrictEventEntryType = other.RestrictEventEntryType;
		}
		
		// Overridden Methods
		//protected override ITweakedEvent<TEH> CreateNew(IList<IEventEntry<TEH>> entries)
		protected override ITweakedEvent<TEH> CreateNew(IEventEntry<TEH>[] entries)
		{
			return new SyncedEvent<TEH>(this, entries);
		}
		
		public override IEventEntry<TEH> CreateEntry(TEH handler)
		{
			//return new SyncedEventEntry<TEH>(handler, this.SyncInvoke);
			return CreateEntry(handler, this.SyncronizerObject);
		}

		/// <summary>
		/// Supported syncObj types are: <code>ISynchronizeInvoke</code>, <code>SynchronizationContext</code>, <code>Dispatcher</code>, and <code>DispatcherObject</code>
		/// A null syncObj is treated as SynchronizationContext.Current
		/// </summary>
		public static SyncedEventEntry<TEH> CreateEntry(IEventEntry<TEH> entry, object syncObj)
		{
			if (entry == null) throw new ArgumentNullException("entry");

			if (syncObj == null) return new SyncContextEventEntry<TEH>(entry);//throw new ArgumentNullException("syncObj");

			if (syncObj is ISynchronizeInvoke)		return new SyncInvokeEventEntry<TEH>(entry, (ISynchronizeInvoke)syncObj);
			if (syncObj is SynchronizationContext)	return new SyncContextEventEntry<TEH>(entry, (SynchronizationContext)syncObj);
			//if (syncObj is Dispatcher)				return new DispatcherEventEntry<TEH>(entry, (Dispatcher)syncObj);
			//if (syncObj is DispatcherObject)		return new DispatcherObjEventEntry<TEH>(entry, (DispatcherObject)syncObj);

			throw new ArgumentException("syncObj argument is not of a supported type", "syncObj");
		}

		/// <summary>
		/// Supported syncObj types are: <code>ISynchronizeInvoke</code>, <code>SynchronizationContext</code>, <code>Dispatcher</code>, and <code>DispatcherObject</code>
		/// A null syncObj is treated as SynchronizationContext.Current
		/// </summary>
		public static SyncedEventEntry<TEH> CreateEntry(TEH handler, object syncObj)
		{
			if (handler == null) throw new ArgumentNullException("handler");

			if (syncObj == null) return new SyncContextEventEntry<TEH>(handler);//throw new ArgumentNullException("syncObj");

			if (syncObj is ISynchronizeInvoke)		return new SyncInvokeEventEntry<TEH>(handler, (ISynchronizeInvoke)syncObj);
			if (syncObj is SynchronizationContext)	return new SyncContextEventEntry<TEH>(handler, (SynchronizationContext)syncObj);
			//if (syncObj is Dispatcher)				return new DispatcherEventEntry<TEH>(handler, (Dispatcher)syncObj);
			//if (syncObj is DispatcherObject)		return new DispatcherObjEventEntry<TEH>(handler, (DispatcherObject)syncObj);

			throw new ArgumentException("syncObj argument is not of a supported type", "syncObj");
		}

		public override ITweakedEvent<TEH> Combine(IEventEntry<TEH> entry)
		{
			if (RestrictEventEntryType && !(entry is SyncedEventEntry<TEH>)) {
				throw new NotSupportedException("Only entries of type SyncedEventEntry<> are supported by this custom event");
			}
			return base.Combine(entry);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) ThoughtWorks
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions