Click here to Skip to main content
Click here to Skip to main content

Safe Trigger Extensions

, 4 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This code provides SafeTrigger extension methods as a way of triggering events in a thread safe way.For details on usage, a discussion of reasons and alternatives, and a full solution download see my article on safely triggering events[^]Multiple overloads are provided for convenience to...
This code provides SafeTrigger extension methods as a way of triggering events in a thread safe way.
 
For details on usage, a discussion of reasons and alternatives, and a full solution download see my article on safely triggering events[^]
 
Multiple overloads are provided for convenience to allow for as many triggering scenarios as possible.
 
/// <summary>
 /// Class that provides SafeTrigger extension methods as a way of triggering events in a thread safe way.
 /// </summary>
 public static class SafeTriggerExtensions
 {
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     public static void SafeTrigger(this EventHandler eventToTrigger, Object sender)
     {
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, EventArgs.Empty);
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     /// <param name="eventArgs">The arguments for the event.</param>
     public static void SafeTrigger(this EventHandler eventToTrigger, Object sender, EventArgs eventArgs)
     {
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, eventArgs);
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <typeparam name="TReturnType">The return type of the event trigger.</typeparam>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     /// <param name="eventArgs">The arguments for the event.</param>
     /// <param name="retrieveDataFunction">A function used to retrieve data from the event.</param>
     /// <returns>Returns data retrieved from the event arguments.</returns>
     public static TReturnType SafeTrigger<TReturnType>(this EventHandler eventToTrigger, Object sender, 
                                                        EventArgs eventArgs, Func<EventArgs, TReturnType> retrieveDataFunction)
     {
         if (retrieveDataFunction == null)
         {
             throw new ArgumentNullException("retrieveDataFunction");
         }
 
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, eventArgs);
             TReturnType returnData = retrieveDataFunction(eventArgs);
             return returnData;
         }
         else
         {
             return default(TReturnType);
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     /// <param name="eventArgs">The arguments for the event.</param>
     public static void SafeTrigger<TEventArgs>(this EventHandler<TEventArgs> eventToTrigger, Object sender, 
                                                TEventArgs eventArgs) where TEventArgs : EventArgs
     {
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, eventArgs);
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     public static void SafeTrigger<TEventArgs>(this EventHandler<TEventArgs> eventToTrigger, 
                                                Object sender) where TEventArgs : EventArgs, new()
     {
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, new TEventArgs());
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
     /// <typeparam name="TReturnType">The return type of the event trigger.</typeparam>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     /// <param name="retrieveDataFunction">A function used to retrieve data from the event.</param>
     /// <returns>Returns data retrieved from the event arguments.</returns> 
     public static TReturnType SafeTrigger<TEventArgs, TReturnType>(this EventHandler<TEventArgs> eventToTrigger, 
                                                                    Object sender, 
                                                                    Func<TEventArgs, TReturnType> retrieveDataFunction) 
                                                                    where TEventArgs : EventArgs, new()
     {
         if (retrieveDataFunction == null)
         {
             throw new ArgumentNullException("retrieveDataFunction");
         }
 
         if (eventToTrigger != null)
         {
             TEventArgs eventArgs = new TEventArgs();
             eventToTrigger(sender, eventArgs);
             TReturnType returnData = retrieveDataFunction(eventArgs);
             return returnData;
         }
         else
         {
             return default(TReturnType);
         }
     }
 
     /// <summary>
     /// Safely triggers an event.
     /// </summary>
     /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
     /// <typeparam name="TReturnType">The return type of the event trigger.</typeparam>
     /// <param name="eventToTrigger">The event to trigger.</param>
     /// <param name="sender">The sender of the event.</param>
     /// <param name="eventArgs">The arguments for the event.</param>
     /// <param name="retrieveDataFunction">A function used to retrieve data from the event.</param>
     /// <returns>Returns data retrieved from the event arguments.</returns> 
     public static TReturnType SafeTrigger<TEventArgs, TReturnType>(this EventHandler<TEventArgs> eventToTrigger, 
                                                                    Object sender, TEventArgs eventArgs, 
                                                                    Func<TEventArgs, TReturnType> retrieveDataFunction) 
                                                                    where TEventArgs : EventArgs
     {
         if (retrieveDataFunction == null)
         {
             throw new ArgumentNullException("retrieveDataFunction");
         }
 
         if (eventToTrigger != null)
         {
             eventToTrigger(sender, eventArgs);
             TReturnType returnData = retrieveDataFunction(eventArgs);
             return returnData;
         }
         else
         {
             return default(TReturnType);
         }
     }
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Simon P Stevens
Software Developer
United Kingdom United Kingdom
I discovered programming aged 11 with my school's BBC micro, and a book titled "Write your own space action games". (Their ideas of "space action" games were very different to mine. My ideas didn't include a bunch of * signs controlled via a text based menu)
 
I got hooked on VB for a while (mainly because I discovered I could replace the * signs with .bmp files) until someone pointed out the wonderful world of objects and Java. I also went thought a c++ phase.
 
I've now been a professional developer for 5 years.
 
My current language of choice is C#. I spend my free time playing with XNA and microcontrollers.
Follow on   Twitter

Comments and Discussions

 
Questiontip/trick: Safe Trigger Extensions Pinmemberterrybozzio11-Jun-13 6:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150224.1 | Last Updated 4 Mar 2010
Article Copyright 2010 by Simon P Stevens
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid