{"id":830,"date":"2008-08-15T08:45:05","date_gmt":"2008-08-15T07:45:05","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/830-silverlight-2-threading-issues-quickstarts-not-working.html"},"modified":"2008-08-15T08:45:05","modified_gmt":"2008-08-15T07:45:05","slug":"silverlight-2-threading-issues-quickstarts-not-working","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/830-silverlight-2-threading-issues-quickstarts-not-working.html","title":{"rendered":"Silverlight 2 threading issues, Quickstarts not working"},"content":{"rendered":"<p>I\u2019ve been working on a Silverlight tutorial involving reading an RSS feed. Silverlight has a SyndicationFeed class which is meant to make this easy \u2013 as Microsoft\u2019s Scott Barnes enthuses <a href=\"http:\/\/blogs.msdn.com\/msmossyblog\/archive\/2008\/08\/14\/do-you-know-silverlight-has-an-inbuilt-native-rss-reader.aspx\">here<\/a>.<\/p>\n<\/p>\n<\/p>\n<\/p>\n<p>It is handy, but I discovered that the <a href=\"http:\/\/silverlight.net\/Quickstarts\/Remote\/ed86e719-0da0-49e1-a9e1-b31f483070d4.aspx\">Quickstart<\/a> Barnes refers to does not work in Silverlight 2 Beta 2. The Quickstart section on Silverlight.net needs some work. Even if you get to this Quickstart via the link for Silverlight 2 Beta 2 examples <a href=\"http:\/\/silverlight.net\/Quickstarts\/default.aspx\">on this page<\/a>, it is soon apparent that it is actually for Silverlight 2 Beta 1. Click the <a href=\"http:\/\/silverlight.net\/Quickstarts\/RunSample.aspx?sref=Syndication%23TestPage.html&amp;lang=cs\">Run It<\/a> button and you\u2019ll see that it asks for the older runtime.<\/p>\n<p>The code doesn\u2019t work in Beta 2 either; and as so often with thread-y stuff, it\u2019s not immediately clear what\u2019s going wrong. I got a blank page and the following message in the Debug output window in Visual Studio:<\/p>\n<blockquote>\n<p>A first chance exception of type &#8216;System.UnauthorizedAccessException&#8217; occurred in mscorlib.dll<\/p>\n<\/blockquote>\n<p>In situations like this I recommend breaking on all CLR exceptions (Debug \u2013 Exceptions \u2013 check the Thrown box for Common Language Runtime exceptions in Visual Studio). Run again; and this time Visual Studio stops on the line which updates a Silverlight TextBlock:<\/p>\n<blockquote>\n<p>feedcontent.Text += &quot;* &quot; + item.Title.Text + Environment.NewLine<\/p>\n<\/blockquote>\n<p>with the message \u201cInvalid cross-thread access\u201d:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.itwriting.com\/images\/sl_exception.gif\" \/> <\/p>\n<p>Rooting about a bit, I found <a href=\"http:\/\/scorbs.com\/2008\/06\/10\/silverlight-2-beta2\">this post<\/a> from Karen Corby on changes in Silverlight 2 Beta 2:<\/p>\n<blockquote>\n<p>HttpWebRequest\u2019s delegates are called on a new non-UI thread. <\/p>\n<ul>\n<li>Delegates were previously always called on the UI thread. <\/li>\n<li>You must invoke back on to the UI thread if the data you\u2019re retrieving will be consumed by a UI element. <\/li>\n<li>For an example, see the updated <a href=\"http:\/\/scorbs.com\/2008\/04\/05\/silverlight-http-networking-stack-part-1-site-of-origin-communication\/\">networking post series (part one)<\/a>. <\/li>\n<\/ul>\n<\/blockquote>\n<p>What this means is that you have two doses of asynchronous coding to think about if you use HttpWebRequest. First, the request itself; and second, in the code you write for the response handler if it needs to update the UI \u2013 which in most cases it will.<\/p>\n<p>The example referenced by Corby shows a neat solution using a SynchronizationContext object, or you can use the Dispatcher class as <a href=\"http:\/\/www.wilcob.com\/Wilco\/Silverlight\/threading-in-silverlight.aspx\">explained by Wilco Bauwer here<\/a> \u2013 he also draws attention to locking issues. See also <a href=\"http:\/\/wildermuth.com\/2008\/05\/06\/Executing_Code_on_the_UI_Thread_in_Silverlight_2.aspx\">Shawn Wildermuth\u2019s post<\/a> though note that CheckAccess is available despite what is said here.<\/p>\n<p>This adds a significant dose of complexity to Silverlight coding. I\u2019m not sure if any of this will change again in the final release.<\/p>\n<p>I also noticed that VB coders are not well served by the Silverlight examples out there, which are overwhelmingly C#. Looks like this is the language of choice if you want an easy life.<\/p>\n<p><div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ff7ad7cb-dc16-424d-9f68-280def41e942\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/silverlight\" rel=\"tag\">silverlight<\/a>, <a href=\"http:\/\/technorati.com\/tags\/threading\" rel=\"tag\">threading<\/a>, <a href=\"http:\/\/technorati.com\/tags\/httpwebrequest\" rel=\"tag\">httpwebrequest<\/a>, <a href=\"http:\/\/technorati.com\/tags\/visual+studio\" rel=\"tag\">visual studio<\/a>, <a href=\"http:\/\/technorati.com\/tags\/microsoft\" rel=\"tag\">microsoft<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been working on a Silverlight tutorial involving reading an RSS feed. Silverlight has a SyndicationFeed class which is meant to make this easy \u2013 as Microsoft\u2019s Scott Barnes enthuses here. It is handy, but I discovered that the Quickstart Barnes refers to does not work in Silverlight 2 Beta 2. The Quickstart section on &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/830-silverlight-2-threading-issues-quickstarts-not-working.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Silverlight 2 threading issues, Quickstarts not working<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,55,77,80,96],"tags":[],"class_list":["post-830","post","type-post","status-publish","format-standard","hentry","category-internet","category-microsoft","category-silverlight","category-software-development","category-web-authoring"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=830"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/830\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}