<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>You had me at "Hello World!" : Threading</title><link>http://blogs.msdn.com/nikola/archive/tags/Threading/default.aspx</link><description>Tags: Threading</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Calling Web Services and Accessing UI from Timer Event in Silverlight</title><link>http://blogs.msdn.com/nikola/archive/2008/03/13/calling-web-services-and-accessing-ui-from-timer-event-in-silverlight.aspx</link><pubDate>Thu, 13 Mar 2008 23:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8186418</guid><dc:creator>nikola</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/nikola/comments/8186418.aspx</comments><wfw:commentRss>http://blogs.msdn.com/nikola/commentrss.aspx?PostID=8186418</wfw:commentRss><description>&lt;P&gt;If you try accessing UI from a timer event, you'll &lt;STRONG&gt;Exception: Invalid cross-thread access&lt;/STRONG&gt;. This happens because the timer code is running on different thread and trying to access controls on the main thread. Here's how to fix it:&lt;/P&gt;
&lt;P mce_keep="true"&gt;Edit: thanks to &lt;A class=namelink href="http://silverlight.net/members/jackbond.aspx" mce_href="http://silverlight.net/members/jackbond.aspx"&gt;jackbond&lt;/A&gt;&amp;nbsp;on the Silverlight.NET forum &lt;A href="http://silverlight.net/forums/p/11533/36916.aspx#36916" mce_href="http://silverlight.net/forums/p/11533/36916.aspx#36916"&gt;http://silverlight.net/forums/p/11533/36916.aspx#36916&lt;/A&gt;&amp;nbsp;who pointed me in the right direction. &lt;/P&gt;
&lt;P mce_keep="true"&gt;The preferred way to do this is by using DispatcherTimer:&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;DispatcherTimer&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; timer = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DispatcherTimer&lt;/SPAN&gt;();&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;timer.Interval = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TimeSpan&lt;/SPAN&gt;(1000);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;timer.Tick += &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;EventHandler&lt;/SPAN&gt;(timer_Tick);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;timer.Start();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Before I was using this method, which still works but is not as good in this case (harder to read):&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Action&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; _onTimerAction = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Action&lt;/SPAN&gt;(OnTimer);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: #2b91af; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;Timer&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; _timer = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Timer&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;TimerCallback&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;delegate&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; action)&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{ Dispatcher.BeginInvoke(_onTimerAction); }), &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;, 0, 3000);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;private&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; OnTimer()&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;{&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 8pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;// call web service/update UI here&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="BACKGROUND: #d9d9d9; MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-background-themecolor: background1; mso-background-themeshade: 217; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;
&lt;P mce_keep="true"&gt;Also, I learned that Dispatcher.BeginInvoke is very usable for calling functions on the UI thread from another thread, as in the above sample.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8186418" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/nikola/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/nikola/archive/tags/Web+Service/default.aspx">Web Service</category><category domain="http://blogs.msdn.com/nikola/archive/tags/Threading/default.aspx">Threading</category></item></channel></rss>