Click here to Skip to main content
15,885,244 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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JpLabs.TweakedEvents;
using System.Threading;
using System.Diagnostics;

namespace JpLabs.SynchronizedEvent.Demo
{
	public partial class Foo : Form
	{
		private TweakedEvent<EventHandler<AsyncCompletedEventArgs>> TaskCompletedEvent;

		public Foo()
		{
			InitializeComponent();
			
			var funcCreateEntry = (Func<EventHandler<AsyncCompletedEventArgs>,IEventEntry<EventHandler<AsyncCompletedEventArgs>>>) (
				//(handler) => new SyncedEventEntry<EventHandler<AsyncCompletedEventArgs>>(handler, this)
				//(handler) => SyncedEventEntry<EventHandler<AsyncCompletedEventArgs>>.CreateEntry(handler, this)
				(handler) => TweakedEvent.ToWeakSynced(this, handler)
				//(handler) => new WeakEventEntry<EventHandler<AsyncCompletedEventArgs>>(handler)
			);
			TaskCompletedEvent = new FuncTweakedEvent<EventHandler<AsyncCompletedEventArgs>>(funcCreateEntry);

			//TaskCompleted += TweakedEvent.ToSynced<AsyncCompletedEventArgs>(Pop_TaskCompleted, this);
			TaskCompleted += Pop_TaskCompleted;
			TaskCompleted += Pop_TaskCompleted_Two;
		}
		
		private event EventHandler<AsyncCompletedEventArgs> TaskCompleted
		{
			add { TweakedEvent.Add(ref TaskCompletedEvent, value); }
			remove { TweakedEvent.Remove(ref TaskCompletedEvent, value); }
		}

		void Pop_TaskCompleted_Two(object sender, AsyncCompletedEventArgs e)
		{
			//this.Dispose();
			this.SuspendLayout();
			this.ResumeLayout(true);
			
			//var lbl = new Label() { Text = "Arrrgh" };
			//lbl.Dispose();
			//this.Controls.Add(lbl);
			//lbl.BringToFront();

			this.SuspendLayout();
			this.ResumeLayout();
			
			this.Focus();

			this.Show();
		}

		void Pop_TaskCompleted(object sender, AsyncCompletedEventArgs e)
		{
			ShowMessage(e.UserState.ToString());
		}
		
		void ShowMessage(string message)
		{
			message = message + " [" + Thread.CurrentThread.ManagedThreadId + "]";

			lstLog.Items.Add(message);
			
			if (this.IsDisposed) {
				System.Diagnostics.Debug.Print(message);
				
				//MessageBox.Show(message);
			}
		}
		
		
		//private event EventHandler<AsyncCompletedEventArgs> 
		
        //public event EventHandler<AsyncCompletedEventArgs> TaskCompleted;
		//{
		//    add { DoorOpenedEvent += value; }
		//    remove { DoorOpenedEvent -= value; }
		//}

		private void btnRun_Click(object sender, EventArgs e)
		{
            var f = (Action<int>)(
				(timeout) => {
					Thread.Sleep(timeout);
					
					try
					{
						TaskCompletedEvent.Raise(this, new AsyncCompletedEventArgs(null, false, "Task completed"));
					}
					catch (Exception ex)
					{
					    System.Diagnostics.Debug.Print(ex.ToString());
					    
						if (Debugger.IsAttached) Debugger.Break();

					    MessageBox.Show(ex.Message);
					}
				}
            );
            
            {
				int timeout = new Random().Next(500, 3000);
	            
				ShowMessage("Task started (" + timeout.ToString() + ")");
	            
				var asyncResult = f.BeginInvoke(timeout, null, null);
				//f.EndInvoke(asyncResult);
			}
		}
	}
}

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