ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

InvokeAction

A set of Dispatcher extenstions that make it easy to cleanly queue lambdas on the Dispatcher.

Source

internal static class DispatcherExtensions
{
    /// <summary>
    /// Invokes the specified <paramref name="action"/> on the given <paramref name="dispatcher"/>.
    /// </summary>
    /// <param name="dispatcher">The dispatcher on which the <paramref name="action"/> executes.</param>
    /// <param name="action">The <see cref="Action"/> to execute.</param>
    /// <param name="priority">The <see cref="DispatcherPriority"/>.  Defaults to <see cref="DispatcherPriority.ApplicationIdle"/></param>
    public static void InvokeAction(this Dispatcher dispatcher, Action action, DispatcherPriority priority)
    {
        if (dispatcher == null)
            throw new ArgumentNullException("dispatcher");
        if (action == null)
            throw new ArgumentNullException("action");
        dispatcher.Invoke(action, priority);
    }
    /// <summary>
    /// Invokes the specified <paramref name="action"/> on the given <paramref name="dispatcher"/>.
    /// </summary>
    /// <typeparam name="T">The type of the argument of the <paramref name="action"/>.</typeparam>
    /// <param name="dispatcher">The dispatcher on which the <paramref name="action"/> executes.</param>
    /// <param name="action">The <see cref="Action{T}"/> to execute.</param>
    /// <param name="arg">The first argument of the action.</param>
    /// <param name="priority">The <see cref="DispatcherPriority"/>.  Defaults to <see cref="DispatcherPriority.ApplicationIdle"/></param>
    public static void InvokeAction<T>(this Dispatcher dispatcher, Action<T> action, T arg, DispatcherPriority priority = DispatcherPriority.ApplicationIdle)
    {
        if (dispatcher == null)
            throw new ArgumentNullException("dispatcher");
        if (action == null)
            throw new ArgumentNullException("action");
        dispatcher.Invoke(action, priority, arg);
    }
    /// <summary>
    /// Invokes the specified <paramref name="action"/> on the given <paramref name="dispatcher"/>.
    /// </summary>
    /// <typeparam name="T1">The type of the first argument of the <paramref name="action"/>.</typeparam>
    /// <typeparam name="T2">The type of the second argument of the <paramref name="action"/>.</typeparam>
    /// <param name="dispatcher">The dispatcher on which the <paramref name="action"/> executes.</param>
    /// <param name="action">The <see cref="Action{T1,T2}"/> to execute.</param>
    /// <param name="arg1">The first argument of the action.</param>
    /// <param name="arg2">The second argument of the action.</param>
    /// <param name="priority">The <see cref="DispatcherPriority"/>.  Defaults to <see cref="DispatcherPriority.ApplicationIdle"/></param>
    public static void InvokeAction<T1, T2>(this Dispatcher dispatcher, Action<T1, T2> action, T1 arg1, T2 arg2, DispatcherPriority priority = DispatcherPriority.ApplicationIdle)
    {
        if (dispatcher == null)
            throw new ArgumentNullException("dispatcher");
        if (action == null)
            throw new ArgumentNullException("action");
        dispatcher.Invoke(action, priority, arg1, arg2);
    }
    /// <summary>
    /// Invokes the specified <paramref name="action"/> on the given <paramref name="dispatcher"/>.
    /// </summary>
    /// <typeparam name="T1">The type of the first argument of the <paramref name="action"/>.</typeparam>
    /// <typeparam name="T2">The type of the second argument of the <paramref name="action"/>.</typeparam>
    /// <typeparam name="T3">The type of the third argument of the <paramref name="action"/>.</typeparam>
    /// <param name="dispatcher">The dispatcher on which the <paramref name="action"/> executes.</param>
    /// <param name="action">The <see cref="Action{T1,T2,T3}"/> to execute.</param>
    /// <param name="arg1">The first argument of the action.</param>
    /// <param name="arg2">The second argument of the action.</param>
    /// <param name="arg3">The third argument of the action.</param>
    /// <param name="priority">The <see cref="DispatcherPriority"/>.  Defaults to <see cref="DispatcherPriority.ApplicationIdle"/></param>
    public static void InvokeAction<T1, T2, T3>(this Dispatcher dispatcher, Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, DispatcherPriority priority = DispatcherPriority.ApplicationIdle)
    {
        if (dispatcher == null)
            throw new ArgumentNullException("dispatcher");
        if (action == null)
            throw new ArgumentNullException("action");
        dispatcher.Invoke(action, priority, arg1, arg2, arg3);
    }
}

Example

// old way
dispatcher.Invoke((Action<string>)((x) => { Console.Write(x); }), "annoying");
// this way
dispatcher.InvokeAction(x=>Console.Write(X), "yay lol");

Author: Will Sullivan

Submitted on: 14 okt. 2010

Language: C#

Type: System.Windows.Threading.Dispatcher

Views: 4851