Wednesday, 7 October 2015

DispatcherUtil - Elegant WPF Dispatcher programmatic handling

Don't you hate it when you need to tweak the Dispatcher to do what you expect and respect the main GUI thread can only work with GUI controls rule? Wouldn't you like to have some code that just does the things you want to do with the Dispatcher without getting the dreaded InvalidOperationException? Well here is the DispatcherUtil I use to achieve this!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Threading;

namespace SomeAcme.SomePackage
{

    /// <summary>
    /// Contains helper methods for dispatcher operations 
    /// </summary>
    public static class DispatcherUtil
    {

        private static readonly List<DelayedAction> actionsRegistered = new List<DelayedAction>();


        /// <summary>
        /// Executes an action passed into this method by a timeout measured in milliseconds
        /// </summary>
        /// <param name="executeAction">Action to execute</param>
        /// <param name="timeOut">The timeout to wait before executing (in milliseconds)</param>
        /// <param name="priority"></param>
        public static bool DelayedInvokeAction(Action executeAction, int timeOut, DispatcherPriority priority = DispatcherPriority.Background)
        {
            var delayedAction = new DelayedAction(executeAction, timeOut);
            actionsRegistered.Add(delayedAction);
            DispatcherTimer dtimer = new DispatcherTimer(priority); 
            dtimer.Interval += new TimeSpan(0, 0, 0, 0, timeOut);
            dtimer.Tag = delayedAction.ExecuteGuid;
            dtimer.Tick += DelayedInvokeTimerTick;
            dtimer.IsEnabled = true;
            dtimer.Start();

            return true;
        }

        private static void DelayedInvokeTimerTick(object sender, EventArgs e)
        {
            var dtimer = sender as DispatcherTimer;
            if (dtimer != null)
            {
                dtimer.IsEnabled = false;
                dtimer.Stop();
                dtimer.Tick -= DelayedInvokeTimerTick; //unsubscribe
                Guid targetActionGuid = (Guid)dtimer.Tag;

                DelayedAction delayedAction = actionsRegistered.Single(a => a.ExecuteGuid == targetActionGuid);
                delayedAction.ActionToExecute(); //now execute the action 
                actionsRegistered.Remove(delayedAction); 

                if (dtimer != null)
                    dtimer = null; //ensure free up dispatcher timer - do not starve threading resources 
            } //if 
        }

        /// <summary>
        /// Invokes an action on the current dispatcher, used to execute operations on the GUI thread
        /// </summary>
        /// <param name="executeAction">The action to execute, pass in e.g. delegate { --code lines goes here } </param>
        /// <param name="dispatcherPriority">The priority to give the action on the thread (signal to the WPF messaging queue). Default is background.</param>
        /// <returns>Returns true when the action was dispatched</returns>
        /// <remarks>Default priority is DispatcherPriority.Background</remarks>
        public static bool InvokeAction(Action executeAction, DispatcherPriority dispatcherPriority = DispatcherPriority.Background)
        {
            Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
            {
                executeAction();
            }), dispatcherPriority);
            return true;
        }

        /// <summary>
        /// Asynchronously invokes an action on the current dispatcher, used to execute operations on the GUI thread
        /// </summary>
        /// <param name="executeAction">The action to execute, pass in e.g. delegate { --code lines goes here } </param>
        /// <param name="dispatcherPriority">The priority to give the action on the thread (signal to the WPF messaging queue). Default is background.</param>
        /// <returns>Returns true when the action was dispatched</returns>
        /// <remarks>Default priority is DispatcherPriority.Background</remarks>
        public static bool BeginInvokeAction(Action executeAction, DispatcherPriority dispatcherPriority = DispatcherPriority.Background)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                executeAction();
            }), dispatcherPriority);
            return true;
        }

        public static void AsyncWorkAndUIThreadUpdate<T>(Dispatcher currentDispatcher, Func<T> threadWork, Action<T> guiUpdate)
        {
            ThreadPool.QueueUserWorkItem(delegate(object state)
            {
                T resultAfterThreadWork = threadWork();
                currentDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<T>(delegate(T result)
                {
                    guiUpdate(resultAfterThreadWork);
                }), resultAfterThreadWork);

            });
        }

    }
}


Enjoy the code! It has helped me many times when I want to do work in other threads or using Tasks with TPL!

2 comments:

  1. In case you are interested in making money from your visitors with popunder advertisments - you can run with one of the biggest companies: Clickadu.

    ReplyDelete
  2. Did you know that that you can earn cash by locking selected sections of your blog or website?
    Simply join Ad Work Media and implement their Content Locking plug-in.

    ReplyDelete