Hi,

I'm developing a very large application, with very large, and lasting processes, so I made a number of test bed applets to test especific parts of the core logic, before introducing them to the main build tree.  Since these algorithms I want to test are long lasting, depending on large datasets and network communications I wanted to take advantege of the new parallel functionallity (and multithreading in general) in .net.

However as test beds and proof of concepts went on, I found myself expending much more time solving the anoying updating-UI-from-another-thread problem than the very problem I wanted to test.  So I came up with a solution that while not optimal it serve me well to focus on solving the problem at hand and not the silly issues of UI thread affinity.

The solucion I'm using leverages extention methods to invoke (get or setting properties) on UI objects without minding from what thread is the calling, like this:

Given that uxStatus is an TextBlock, and uxSelection a combobox, do the following;

uxStatus.Setter("Text","Working...");

or

var item=uxSelection.Getter("SelectedItem")

So without much ado here it is:

     public static class DispatcherHelper
    {
        private static Action<FrameworkElement, string, object> _setterDelegate;
        private static Func<FrameworkElement, string, object> _getterDelegate;
        static DispatcherHelper()
        {
            _setterDelegate = new Action<FrameworkElement, string, object>(Setter);
            _getterDelegate = new Func<FrameworkElement, string, object>(Getter);
        }
        public static void Setter(this FrameworkElement control, string propertyName, object value)
        {
            if (control.Dispatcher.CheckAccess())
            {
                Type type = control.GetType();
                var propertyInfo = type.GetProperty(propertyName);
                propertyInfo.SetValue(control, value, null);
            }
            else
            {
                control.Dispatcher.Invoke(_setterDelegate, control, propertyName, value);
            }
        }
        public static object Getter(this FrameworkElement control, string propertyName)
        {
            if (control.Dispatcher.CheckAccess())
            {
                Type type = control.GetType();
                var propertyInfo = type.GetProperty(propertyName);
                return propertyInfo.GetValue(control, null);
            }
            else
            {
                return control.Dispatcher.Invoke(_getterDelegate, control, propertyName);
            }
        }
        public static void Invoke(this FrameworkElement control, Action func)
        {
            control.Dispatcher.Invoke(func);
        }
        public static void Invoke(this FrameworkElement control, Delegate func, params object[] args)
        {
            control.Dispatcher.Invoke(func, args);
        }
    }

It consists of a couple of extention methods.  It has served me so well that I'm considering to introducing let me know what you think about it.  Believe me this methods have spared me a lot of code.

Raptor