Click here to Skip to main content
15,879,613 members
Articles / Desktop Programming / Windows Forms

An Easy to Use Weak Referenced Event Handler Factory for .NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.84/5 (19 votes)
9 Mar 200710 min read 151.8K   931   87  
An article on creating weak event handlers that show how to use them in a Windows Form.
// ****************************************************************************
// Copyright Swordfish Computing Australia 2005                              **
// http://www.swordfish.com.au/                                              **
//                                                                           **
// Filename: Swordfish\Collections\WeakEventHandler.cs                       **
// Authored by: John Stewien of Swordfish Computing                          **
// Date: September 2005                                                      **
//                                                                           **
// - Change Log -                                                            **
// ****************************************************************************
// $Id: WeakEventHandler.cs,v 1.5 2005/10/11 01:43:22 stewienj Exp $

using System;
using System.Collections.Generic;
using System.Text;

namespace Swordfish.Collections
{
	/// <summary>
	/// Use this only when you know what you are doing. All weak event handlers
	/// must be removed before the host object is garbage collected, otherwise
	/// the underlying WeakReferenceToEventHandler will bomb when it next handles
	/// an event.
	/// </summary>
	/// <typeparam name="TEventArgs"></typeparam>
	public class WeakEventHandler<TEventArgs> : WeakEventHandlerInternal<TEventArgs> where TEventArgs : EventArgs
	{
		// ********************************************************************
		// Private Fields
		// ********************************************************************
		#region Private Fields

		/// <summary>
		/// Pre-calculated event handler for fast adding and removing of an event handler
		/// </summary>
		private EventHandler<TEventArgs> substituteHandler;

		#endregion Private Fields

		// ********************************************************************
		// Public Methods
		// ********************************************************************
		#region Public Methods

		/// <summary>
		/// Constructor. Initializes all the fields.
		/// </summary>
		/// <param name="eventSource"></param>
		public WeakEventHandler(EventHandler<TEventArgs> strongEventHandler) : base(strongEventHandler)
		{
			substituteHandler = new EventHandler<TEventArgs>(weakEventHandler.IntermediateEventHandler);
		}

		/// <summary>
		/// Implicit conversion allowing this object to be used in place of
		/// an event handler
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static implicit operator EventHandler<TEventArgs>(WeakEventHandler<TEventArgs> weakEventHandler)
		{
			return weakEventHandler.substituteHandler;
		}

		#endregion Public Methods
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Cheesy Design
Taiwan Taiwan
John graduated from the University of South Australia in 1997 with a Bachelor of Electronic Engineering Degree, and since then he has worked on hardware and software in many fields including Aerospace, Defence, and Medical giving him over 10 of years experience in C++ and C# programming. In 2009 John Started his own contracting company doing business between Taiwan and Australia.

Comments and Discussions