Asynchronous Silverlight WCF callback

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-26T05:35:54Z Indexed on 2010/03/26 5:43 UTC
Read the original article Hit count: 416

I've created my own WCF service and I've successfully been able to talk to it via my Silverlight client. I ran into an interesting problem on my asynchronous callbacks though. When my callback is invoked, I can't update any UI controls with the dreaded invalid cross thread access

Here's what my callback function looks like

    private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
    {
        lblDisplay.Text = e.Object.ToString();
    }

A quick google search showed me that I have to do this instead.

private void GetTimeCallBack( object sender, Talk.ClientBase<IService>.ClientEventArgs e )
    {
        Dispatcher.BeginInvoke( () => lblDisplay.Text = e.Object.ToString() );
    }

Now everything works fine, but I wasn't expecting my callback to be running on a different thread. Will I always have to use the Dispatcher class in order to modify anything within my class or is this just limited to UI elements? I've not familiar with the Dispatcher class at all so I'm looking to understand it more.

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about wcf