Hey all

I'm having trouble updating my ui across threads using WPF. Having learnt a fair deal about thread yesterday, I find myself stumped now by the Dispatcher. I've already used the dispatcher once, to check the status of a radio button, as follows:

if ((Boolean)this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new noArgsDelegate(productIsChecked)))
            {
                //...do some work...
            }

...with productIsChecked looking as follows:

private Boolean productIsChecked()
        {
            return (Boolean)radProduct.IsChecked;
        }

This worked nicely. Then, in an attempt to change the status of one of my tab items (placing a graph as the contents), I tried the following:

this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new noArgsDelegate(setGraph));

...where setGraph() looks as follows:

private Boolean setGraph()
        {
            try
            {
                GraphArea.Content = c;
                MessageBox.Show("Graph Set");
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
        }

(Sorry there's so much code. Just wanted to give a full idea of what's going on) When I run this, I get a TargetInvocationException. Suspecting what was happening, I stuck in the try catch you can see, and, as I expected... I was getting an InvalidOperationException. I can't change the content of the GraphArea since it belongs to another thread... but I thought the Dispatcher dealt with this... Any idea of what's going wrong, or how to fix it?

Thanks
M

I think it's problem regarding to cast something. Try it with following code,

{
    if(this.Dispatcher.Thread==Thread.CurrentThread) {
                GraphArea.Content = c;
       }
                MessageBox.Show("Graph Set");
                return true;
           
     }

Duplicate thread : http://www.daniweb.com/forums/thread206895.html should be removed.

Hmmm

no luck. I'll keep looking though. Thanks for your help

Is there any way to help you? Post complete source code. (Use attachment)

Hey don't worry, a friend of mine can sort this out for me. Thanks for all the help though, I appreciate it. Not sure I like the Dispatcher much anymore lol

Thanks
M

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.