DCSIMG
ISynchronizedInvoke and WPF - David Sackstein's Blog

ISynchronizedInvoke and WPF

I have seen a lot of discussion out there about why Dispatchers in WPF don’t implement ISynchronizedInvoke.

I don’t have the answer, but I can offer you an extension method I use to simplify the synchronization of all those asynchronous events onto a window thread.

Here is the extension method:

    public static class Extensions

    {

        static public void InvokeIfRequired(this Dispatcher dispatcher, Action action)

        {

            if (dispatcher.CheckAccess())

            {

                action();

            }

            else

            {

                dispatcher.BeginInvoke(DispatcherPriority.Normal, action);

            }

        }

    }

And you can use it like this:

    void task_CompletedEvent(object sender, EventArgs args)

    {

        // Possibly called from another thread

 

        Dispatcher.InvokeIfRequired(() =>

        {

            // You can access the UI here, we are in the window thread

        });

    }

Published Monday, April 27, 2009 1:26 AM by David Sackstein

Comments

# Topics about Microsoft » ISynchronizedInvoke and WPF

Monday, April 27, 2009 5:00 AM by Topics about Microsoft » ISynchronizedInvoke and WPF

Pingback from  Topics about Microsoft  » ISynchronizedInvoke and WPF

# re: ISynchronizedInvoke and WPF

Tuesday, May 05, 2009 5:47 PM by Gilad Lavian

Thanks!

# http://windowsclient.net/community/

Tuesday, May 19, 2009 11:39 AM by TrackBack

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems