Two useful generic utility classes for .Net applications

Posted on August 5, 2007

    /// <summary>
    /// By Jean-Paul S. Boodhoo
    /// From http://blog.codebetter.com/blogs/jean-paul_boodhoo/archive/2007/07/11/165483.aspx#165493
    /// </summary>
    /// <typeparam name="T">event specific data type</typeparam>
    public class EventArgse<T> : EventArgs where T : new()
    {
        public new static readonly EventArgse<T> Empty;
        private readonly T eventData;

        static EventArgse()
        {
            Empty = new EventArgse<T>();
        }

        private EventArgse()
        {
        }

        public EventArgse(T eventData)
        {
            this.eventData = eventData;
        }

        public T EventData
        {
            get { return eventData; }
        }
    }

     /// <summary>
    /// By Nate Kohari
    /// From http://kohari.org/2007/08/03/thread-safe-event-handlers/
    /// </summary>
    public static class ThreadSafe
    {
        public static EventHandler<T> EventHandler<T>(Control context, EventHandler<T> handler)
        where T : EventArgs
        {
            return delegate(object sender, T args)
            {
                if (context.InvokeRequired)
                    context.Invoke(handler, sender, args);
                else
                    handler(sender, args);
            };
        }
        public static EventHandler EventHandler(Control context, EventHandler handler)
        {
            return delegate(object sender, EventArgs args)
            {
                if (context.InvokeRequired)
                    context.Invoke(handler, sender, args);
                else
                    handler(sender, args);
            };
        }
    }

Filed Under Uncategorized | Leave a Comment

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name

Email

Website

Comments

© Copyright 0xDEADBEEFCAFE • Powered by Wordpress • Design by Sebastin.

free web hit counter