Silverlight wont update UI from background thread

by Bobbi Perreault 25. May 2008 09:16
Share on Facebook

When I did this:  Used a web service to retrieve an XML serialized list of items from my database, and used the deserialized list to fill a member variable that was a list.

This happened:  Within the method which was called from the asyncronous retrieve - the member variable that was a list was filled with the appropriate number of class instances.

As soon as that same list was accessed from an event handler, the count would again show 0 items as if there never were any items from the retrieve.

Here's what I found out about it, and how I fixed it: 
In Silverlight applications, you aren't allowed to modify the UI thread from a background thread.  Of course, the last event of an asyncronous call to a web service isn't on the UI thread.  This line of code insures that the method which fills  my member vars is executed on the UI thread:

Dispatcher.BeginInvoke(() => setMappedItems());


That is, setMappedItems() is the method which fills the member list.

Walla, no more missing items in my list.   I found this useful information in this blog here:  http://adoguy.com/2008/05/06/Executing_Code_on_the_UI_Thread_in_Silverlight_2.aspx

Link to Source Code Formatter

by Bobbi Perreault 19. April 2008 09:28
Share on Facebook

http://blogs.msdn.com/delay/archive/2008/03/13/blogging-code-samples-should-be-easy-free-convertclipboardrtftohtmltext-tool-and-source-code.aspx

Opening text of this post:  

Blogging code samples should be easy [Free ConvertClipboardRtfToHtmlText tool and source code!]

I've been including a lot of source code examples in my blog lately and needed a good way to paste properly formatted code in blog posts. I write my posts in HTML and wanted a tool that was easy to use, simple to install, produced concise HTML, and worked well with Visual Studio 2008. I was aware of a handful of tools for this purpose but none of them were quite what I was looking for, so I wrote my own one evening. :)

Tags:

StickyPad

Link to Silverlight Getting Started Information

by Bobbi Perreault 18. April 2008 23:43
Share on Facebook

For you if you haven't had an opportunity to get started with Silverlight yet. Here's a great "how to start working with Silverlight" blog entry: http://www.danielmoth.com/Blog/2008/04/silverlight-2-essential-getting-started.html

Belize - Paintings by BCP

Tags:

StickyPad

Wilco Bauwer - Threading in Silverlight

by Bobbi Perreault 17. April 2008 22:21
Share on Facebook

Just yesterday when I was working on a WPF application, I was searching for a simple introduction to the new way to do threading.  I had no luck with that, there were plenty of tutorials, just none that I could skim through to pick up the light details I needed.  And time is always so short! 

This morning, though, I had in my RSS feed just what I needed.  Here it is: http://wilcoding.xs4all.nl/Wilco/News/threading-in-silverlight.aspx

Threading in Silverlight

Posted at Thu, 17 Apr 2008 00:09:06 GMT by Wilco Bauwer

Silverlight 2 brings support for threading to the browser. You can either directly start new threads using System.Threading.Thread and System.Threading.ThreadPool, or you can use the higher-level (and recommended) System.ComponentModel.BackgroundWorker type. The latter encapsulates the concept of executing work in the background (using a thread from the thread-pool) and updating the UI based on progress and/or completion of that work, which means that you can safely update the UI from the related events.

WPF version:

CallStatDisplay.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(delegate { myTextBlock.Text = "Updated from a non-UI thread."; }));

Actually WPF uses dispatcher threading model which means the ui elements in WPF are dispatcher affinitized, you can only perform operations on UI elements in the dispatcher which creates them. since dispatcher is affinitized with a specific STA thread, so WPF still uses the single threaded model which Windows Forms employ, for more info on WPF's threading model please refer to:

http://msdn2.microsoft.com/en-us/library/ms741870.aspx

Tags:

StickyPad

Making Life Easier in Visual Studio on Silverlight Project

by Bobbi Perreault 15. April 2008 22:26
Share on Facebook

I got these tips from a document which came with the Infragistics Faceout application. http://www.infragistics.com/hot/silverlight.aspx#InfragisticsandSilverlight This information answered my prayers, I've been looking for how to do the first tip for SOME TIME now.  (I'm a little slow sometimes.....)

Disable the XAML design surface, so that when you open a XAML file in Visual Studio it opens directly in the XAML editor.  The Silverlight XAML design surface is currently read-only, can be slow to load, and sometimes throws unwarranted exceptions.  You can set the XAML editor as the default editor for .xaml files by following these steps:  

I changed my mind on the below advice, here's how I do it now because I found that when I use the option "Open With" I experienced lockups and it was a problem. (Probably just me.)

In any case, this option works much better for me: Use Visual Studio Tools menu, Select Options -- Text Editor -- XAML -- Miscellaneous. And then you will see "Always Open documents in full XAML view" That's the ticket.  Saves so much time. 


1) Right-click on a XAML file in Solution Explorer
2) In the context menu, select ‘Open With…’
3) In the Open With dialog, select ‘Source Code (Text) Editor’
4) Click the ‘Set As Default’ button


When you first open the FaceOut solution, be sure to set the Infragistics.Labs.Silverlight.FaceOut.Web project as the startup project.  You can do this by right clicking on the project node in the Solution Explorer selecting ‘Set as StartUp Project’ from the context menu.   You also must make the main ASPX page in that project the start page, by right-clicking on it and selecting ‘Set as Start Page’ from the context menu.   This is important because these settings are not stored in source control.  If you don’t do this, you will likely get exceptions when calling services on the host server (site of origin). Also, be sure to enable Silverlight debugging if you want to step through the FaceOut code in debug mode.  To enable Silverlight debugging support in Visual Studio 2008, follow these steps:
1) Open Solution Explorer and right-click on the Infragistics.Labs.Silverlight.FaceOut.Web project
2) Select ‘Property Pages’ from the context menu
3) In the Property Pages dialog, select ‘Start Options’ on the left-hand side
4) Check the ‘Silverlight’ checkbox on the right to enable Silverlight debugging
This setting is also not persisted in source control.

Tags:

StickyPad

 

RSS Feed FriendFeed