<?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>Matt Dotson's .NET Tips &amp; Tricks : WinForms</title><link>http://blogs.msdn.com/mattdotson/archive/tags/WinForms/default.aspx</link><description>Tags: WinForms</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Using SynchronizationContexts.</title><link>http://blogs.msdn.com/mattdotson/archive/2006/02/13/531315.aspx</link><pubDate>Tue, 14 Feb 2006 01:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:531315</guid><dc:creator>mattdotson</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/mattdotson/comments/531315.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mattdotson/commentrss.aspx?PostID=531315</wfw:commentRss><wfw:comment>http://blogs.msdn.com/mattdotson/rsscomments.aspx?PostID=531315</wfw:comment><description>&lt;P&gt;SynchronizationContext is new in .NET 2.0, and I haven't found too much written about it yet.&amp;nbsp; It is used by classes like BackgroundWorker and some other built-in classes, and I wanted to show how you can use it in your code as well.&amp;nbsp;&amp;nbsp;I came across this while working on my WinCast project where I needed to handle events coming from the RSS synchronization engine on a background thread.&amp;nbsp;&amp;nbsp;I wanted to handle these events (like a new feed added) by adding new UI elements.&amp;nbsp; If you've done any work in Windows programming, you are probably aware that accessing the UI from a thread that didn't create the UI is a big no no.&amp;nbsp; In .NET 1.1, we could use Invoke on a control to get something executed on the UI thread, but that wasn't the greatest solution.&amp;nbsp; .NET 2.0 brings us SynchoronizationContext which allows us to execute a section of code in the UI context (the thread that created the UI).&amp;nbsp; Also, it allows us to&amp;nbsp;specify if the background thread will block waiting for the UI thread (using Send) or will not block (using Post).&amp;nbsp; You can get the UI context from the WindowsFormsSynchronizationContext.Current property.&amp;nbsp; Here is an admittedly dumb example of a lastUpdatedTime being updated on a background thread, and the UI being properly updated on the UI thread to show the new time in a textbox.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;partial&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;FONT color=#008080&gt;UsingContextForm&lt;/FONT&gt; : &lt;FONT color=#008080&gt;Form&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#008080&gt;SynchronizationContext&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; uiContext;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;System.Timers.&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008080&gt;Timer&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; backgroundTimer;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008080&gt;DateTime&lt;/FONT&gt; lastUpdated = &lt;FONT color=#008080&gt;DateTime&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;.Now;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; UsingContextForm()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Courier New" size=2&gt;// save our ui context for use later&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;uiContext = &lt;/FONT&gt;&lt;FONT face="Courier New" color=#008080&gt;WindowsFormsSynchronizationContext&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;.Current;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;InitializeComponent();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Courier New" size=2&gt;// start a timer on a background thread&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;backgroundTimer = &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; System.Timers.&lt;FONT color=#008080&gt;Timer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;(1000.0);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;backgroundTimer.Elapsed += &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; System.Timers.&lt;FONT color=#008080&gt;ElapsedEventHandler&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;(backgroundTimer_Elapsed);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;backgroundTimer.Start();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;void&lt;/FONT&gt; backgroundTimer_Elapsed(&lt;FONT color=#0000ff&gt;object&lt;/FONT&gt; sender, System.Timers.&lt;FONT color=#008080&gt;ElapsedEventArgs&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; e)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Courier New" size=2&gt;//This executes on a background thread&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;lastUpdated = &lt;/FONT&gt;&lt;FONT face="Courier New" color=#008080&gt;DateTime&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;.Now;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Courier New" size=2&gt;//Tell the ui thread to execute some code for us.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;uiContext.Send(&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#008080&gt;SendOrPostCallback&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;(&amp;nbsp;&lt;BR&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;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff&gt;delegate&lt;/FONT&gt;(&lt;FONT color=#0000ff&gt;object&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; state)&amp;nbsp;&lt;BR&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;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;BR&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;/FONT&gt;&lt;FONT color=#008000&gt;&lt;FONT face="Courier New" size=2&gt;//This executes on the ui thread&lt;BR&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;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;textBox1.Text = lastUpdated.ToLongTimeString();&lt;BR&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;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;}&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;), &lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff&gt;null&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=531315" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mattdotson/archive/tags/WinForms/default.aspx">WinForms</category></item></channel></rss>