Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / threads

Safe Trigger Extensions

5.00/5 (3 votes)
4 Mar 2010CPOL 1  
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.

C#
/// <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)