Multi-threading in Silverlight

by Cameron Albert 14. March 2008 20:40

I've been working with Sockets in Silverlight over the past couple of days, in between getting the Pirates game to a workable Silverlight 2 state. One thing I came across today which sent me round and round was BeginInvoke. Anyone who has programmed multi-threaded applications in Windows has used this, mostly to execute code on the UI thread from events.

Anyway, controls in Silverlight 2 do not have the Invoke or BeginInvoke methods but controls do have a Dispatcher reference which lives in System.Windows.Threading. WPF programmers are probably already used to this but it was new for me. The Dispatcher allows you to check to see if you can access the current object on the current thread.

One thing to note about the Dispatcher is that it has a method called CheckAccess() that returns true if you can access the control on the current thread of false if you are not on the control's thread. The CheckAccess() method is decorated with a EditorBrowsableAttribute and has it's value set to Never. This prevents us from being able to see the method via intellisense in Visual Studio but does not prevent the method from being used. Not sure why the folks at MS wanted to hide this method and the documentation does not provide an explanation. The DependancyObject class also provides a CheckAccess method that is also hidden from the editor and just calls the Dispatcher method of the current object. Maybe it's not documented because it might be removed in the future, it is in beta after all.

So, to sum up, if you need to be able to invoke methods on a control's thread from a background thread you can do the following: (snippet from socket testing)

        private delegate void AppendTextDelegate(string text);
        public void AppendText(string text)
        {
            if (txtChat.CheckAccess())
            {
                // Append the message to the chat buffer.
                _content.Append(text).Append(Environment.NewLine);

                // Re-post the contents of the chat buffer to the chat text and scroll to
                // the bottom.
                txtChat.Content = _content.ToString();
                txtChat.ScrollToVerticalOffset(txtChat.ScrollableHeight);
            }
            else
            {
                txtChat.Dispatcher.BeginInvoke(new AppendTextDelegate(this.AppendText), text);
            }
        }

The _content control is just a StringBuilder that actually holds the content from the chat control.

Again, Silverlight 2 is in beta 1 so maybe this will change in the future.

Tags:

Game Development | General | Silverlight Games | Silverlight 2 Development

Comments

3/15/2008 9:31:56 AM #

Rob

The methods you mention are present in WPF and are pretty central.  I don't think they are going away.  They are also completely viewable through intellisense.  I think the EditorBrowsableAttribute thing was a mistake by the SL team.  Hopefully they will fix this.  There are a lot of things like this that keep cropping up with SL 2.0.

Rob United States

3/15/2008 11:00:03 AM #

pingback

Pingback from geekswithblogs.net

Silverlight Cream for March 14, 2008 - 2 -- #224

geekswithblogs.net

3/15/2008 5:41:02 PM #

calbert

That's good that they are present in WPF, I need to do more WPF progamming Smile

calbert United States

4/18/2008 1:58:34 PM #

pingback

Pingback from robertjantuit.nl

Robs Usability Development : Silverlight, ASP.NET, Ajax, Agile, MVC & Volta  - NL   » Updating the UI Thread in Silverlight 2

robertjantuit.nl

Powered by BlogEngine.NET 1.5.0.7
Modified Theme by Mads Kristensen

About the Author

CameronAlbert.com I am Senior Software Development Consultant specializing in Silverlight, WPF and the Microsoft .NET Framework. 

My current project Perenthia is a Silverlight multi-player game based in a fantasy world that combines text adventure games with some moderate graphics

View Cameron Albert's profile on LinkedIn
See how we're connected

Follow cameronalbert on Twitter

 

Recommended Books

Silverlight 4 Business Application Development - Beginner's Guide:

http://www.packtpub.com/microsoft-silverlight-4-business-application-development-beginners-guide/book

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide