<?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>Kael Rowan</title><link>http://blogs.msdn.com/kaelr/default.aspx</link><description>Foundations of Elegant Code</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Code Canvas @ TechFest</title><link>http://blogs.msdn.com/kaelr/archive/2009/11/09/code-canvas-techfest.aspx</link><pubDate>Mon, 09 Nov 2009 19:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9919726</guid><dc:creator>kaelr</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9919726.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9919726</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9919726</wfw:comment><description>&lt;P&gt;In honor of today's historic date of breaking down walls, I'm posting a recording of the live presentation of Code Canvas given at the 2009 Microsoft Research TechFest event on the Microsoft campus in Redmond, WA.&lt;/P&gt;
&lt;P&gt;This video demonstrates some of the features of Code Canvas that are applicable when zoomed in close to your code, such as grouping by folders, organization by type hierarchy, references and off-screen previews, screen-constrained search results, code coverage overlays, and code execution traces, including multi-threaded execution traces showing thread synchronization.&lt;/P&gt;
&lt;P&gt;
&lt;OBJECT width=533 height=325&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/d4NEaXajiI0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;amp;ap=%2526fmt%3D22"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
&lt;embed src="http://www.youtube.com/v/d4NEaXajiI0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;ap=%2526fmt%3D22" mce_src="http://www.youtube.com/v/d4NEaXajiI0&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;ap=%2526fmt%3D22" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="533" height="325"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9919726" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Code+Canvas/default.aspx">Code Canvas</category></item><item><title>Code Canvas @ Lang.NET Symposium</title><link>http://blogs.msdn.com/kaelr/archive/2009/05/12/code-canvas-lang-net.aspx</link><pubDate>Tue, 12 May 2009 17:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9607812</guid><dc:creator>kaelr</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9607812.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9607812</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9607812</wfw:comment><description>&lt;P&gt;Microsoft recently hosted the &lt;A href="http://langnetsymposium.com/" mce_href="http://langnetsymposium.com"&gt;2009 Lang.NET Symposium&lt;/A&gt; here in the Microsoft Research building.&amp;nbsp; A colleague of mine, &lt;A href="http://blogs.msdn.com/hermanventer" mce_href="http://blogs.msdn.com/hermanventer"&gt;Herman Venter&lt;/A&gt;, asked me to show off a demo of &lt;A href="http://blogs.msdn.com/kaelr/archive/2009/03/26/code-canvas.aspx" mce_href="http://blogs.msdn.com/kaelr/archive/2009/03/26/code-canvas.aspx"&gt;Code Canvas&lt;/A&gt; during his &lt;A href="http://ccimetadata.codeplex.com/" mce_href="http://ccimetadata.codeplex.com/"&gt;Common Compiler Infrastructure&lt;/A&gt; talk, and the video is available online:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.langnetsymposium.com/2009/talks/26-HermanVenter-CCI.html" mce_href="http://www.langnetsymposium.com/2009/talks/26-HermanVenter-CCI.html"&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title="Code Canvas Menagerie" border=0 alt="Code Canvas Menagerie" src="http://blogs.msdn.com/blogfiles/kaelr/WindowsLiveWriter/CodeCanvasLang.NET_98EB/Code%20Canvas%20Menagerie_3.png" width=533 height=329 mce_src="http://blogs.msdn.com/blogfiles/kaelr/WindowsLiveWriter/CodeCanvasLang.NET_98EB/Code%20Canvas%20Menagerie_3.png"&gt; &lt;BR&gt;http://www.langnetsymposium.com/2009/talks/26-HermanVenter-CCI.html&lt;/A&gt;&amp;nbsp;(&lt;A href="http://download.microsoft.com/download/7/6/A/76A69AE5-72B5-4005-BBD9-7EA5F4795014/26-HermanVenter-CCI.wmv" mce_href="http://download.microsoft.com/download/7/6/A/76A69AE5-72B5-4005-BBD9-7EA5F4795014/26-HermanVenter-CCI.wmv"&gt;Download&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;The segment on Code Canvas starts about 5 minutes into the video and lasts for about 2.5 minutes.&amp;nbsp;&amp;nbsp; It also shows off some new features not seen on the previous video.&amp;nbsp; Enjoy!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9607812" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Code+Canvas/default.aspx">Code Canvas</category></item><item><title>Softpedia article on Code Canvas</title><link>http://blogs.msdn.com/kaelr/archive/2009/03/28/softpedia-article-on-code-canvas.aspx</link><pubDate>Sun, 29 Mar 2009 05:56:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9516590</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9516590.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9516590</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9516590</wfw:comment><description>&lt;p&gt;Wow, I’m impressed with how fast the word is getting out.&amp;#160; Marius Oiaga already published an article on Softpedia:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;“What if code could leave the boundaries of the traditional editors, and call home an infinite two-dimensional surface as a new spatial development environment? Code Canvas makes this possible. A project put together by &lt;/em&gt;&lt;a href="http://blogs.msdn.com/kaelr/archive/2009/03/26/code-canvas.aspx"&gt;&lt;strong&gt;&lt;em&gt;Kael Rowan&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;em&gt;, an RSDE in the Human Interactions in Programming group at &lt;/em&gt;&lt;em&gt;Microsoft&lt;/em&gt;&lt;em&gt; Research, Code Canvas is designed to offer developers a spatial layout and Deep Semantic Zoom for source code. And an entirely new perspective over building software. Essentially, Code Canvas is meant to catalyze the evolution of the software development experience via spatial memory.&lt;/em&gt;”&lt;/p&gt;  &lt;p&gt;Read the whole article here: &lt;a href="http://news.softpedia.com/news/Code-Canvas-Spatial-Representations-of-Code-107958.shtml"&gt;Code Canvas: Spatial Representations of Code&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9516590" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Code+Canvas/default.aspx">Code Canvas</category></item><item><title>Code Canvas</title><link>http://blogs.msdn.com/kaelr/archive/2009/03/26/code-canvas.aspx</link><pubDate>Fri, 27 Mar 2009 04:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9512603</guid><dc:creator>kaelr</dc:creator><slash:comments>14</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9512603.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9512603</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9512603</wfw:comment><description>&lt;P&gt;I’ve been working in Microsoft Research on a project called Code Canvas for a while now, and we finally got through enough of the patent process so that I’m able to release some details publically.&lt;/P&gt;
&lt;P&gt;Code Canvas is a research prototype focused on spatial orientation of code as the foundation of an integrated development environment. It is a spatial (2.5D) representation of source code, visual designers, and project-related artifacts that utilizes infinite panning and smooth semantic zoom for navigation. It is also extensible to allow analysis overlays and graph-based relationship visualizations.&lt;/P&gt;
&lt;P&gt;Whenever a developer draws their code on a whiteboard, they are applying a sense of space to their software that includes directional relationships and architectural boundaries. Code Canvas lets developers write their code on a two-dimensional infinite canvas instead of in tabbed editors, so all of their source code is arranged in the same way as it would be on the whiteboard.&amp;nbsp; They can still write code as they do today, in C# or C++ or whatever, but the directional relationships and architectural boundaries are part of the same canvas, and they can easily navigate and zoom smoothly in and out to understand everything at once.&lt;/P&gt;
&lt;P&gt;In addition to utilizing spatial memory, the ability to overlay visualizations among multiple files at once is proving to be extremely valuable, especially when the visualizations are drawn directly on top of the same source code that you have already been working with.&amp;nbsp; This essentially takes the canvas from a simple code editing surface and turns it into a full-blown diagramming surface, but I’ll have to go more into that later…&lt;/P&gt;
&lt;P&gt;In the mean time, here is a somewhat old video of Code Canvas that shows some of the first features I implemented when getting started on this project.&lt;/P&gt;
&lt;P&gt;
&lt;OBJECT width=533 height=325&gt;&lt;PARAM NAME="movie" VALUE="http://www.youtube.com/v/A-3eIbhmoPo&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;amp;ap=%2526fmt%3D22"&gt;&lt;PARAM NAME="allowFullScreen" VALUE="true"&gt;&lt;PARAM NAME="allowscriptaccess" VALUE="always"&gt;
&lt;embed src="http://www.youtube.com/v/A-3eIbhmoPo&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;ap=%2526fmt%3D22" mce_src="http://www.youtube.com/v/A-3eIbhmoPo&amp;amp;hl=en&amp;amp;fs=1&amp;amp;rel=0&amp;amp;fmt=22&amp;ap=%2526fmt%3D22" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="533" height="325"&gt;&lt;/embed&gt;&lt;/OBJECT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9512603" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Code+Canvas/default.aspx">Code Canvas</category></item><item><title>Blog Resurrection</title><link>http://blogs.msdn.com/kaelr/archive/2008/10/15/blog-resurrection.aspx</link><pubDate>Thu, 16 Oct 2008 01:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9000225</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9000225.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9000225</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9000225</wfw:comment><description>&lt;P&gt;Sometimes in the course of software development it becomes best to start over from scratch.&amp;nbsp; I'm taking this approach with my blog.&amp;nbsp; I first joined Microsoft in 2000 as part of the Windows XP Embedded group.&amp;nbsp; In 2004 I joined the Windows Driver Foundation group and created a managed (.NET) interface to the User Mode Driver Framework.&amp;nbsp; That was also when I attempted my first series of blog posts which were going to describe how to write a driver in C# to control a small USB device.&amp;nbsp; My management didn't feel comfortable with blogging at the time and 'encouraged' me to discontinue the series.&amp;nbsp; I left Windows and joined Microsoft Research in 2006 as part of the Foundations of Software Engineering group to work on model-based program&amp;nbsp;analysis and Spec#.&amp;nbsp; I still wanted to contribute to the blogging community, so I started a second set of blog posts that gave solutions to random problems I had solved that I thought may be helpful to others.&amp;nbsp; There wasn't any consistency among the topics and eventually the blog became stagnant again.&lt;/P&gt;
&lt;P&gt;I'm currently an &lt;A title=Research href="http://research.microsoft.com/aboutmsr/jobs/fulltime/technical.aspx" mce_href="http://research.microsoft.com/aboutmsr/jobs/fulltime/technical.aspx"&gt;R&lt;/A&gt;&lt;A title="Software Development/Design Engineer" href="http://blogs.msdn.com/techtalk/archive/2006/02/02/dev-at-microsoft.aspx" mce_href="http://blogs.msdn.com/techtalk/archive/2006/02/02/dev-at-microsoft.aspx"&gt;SDE&lt;/A&gt; in the &lt;A href="http://research.microsoft.com/hip" mce_href="http://research.microsoft.com/hip"&gt;Human Interactions in Programming&lt;/A&gt; group in &lt;A class="" href="http://research.microsoft.com/" mce_href="http://research.microsoft.com"&gt;Microsoft Research&lt;/A&gt;.&amp;nbsp; I'm responsible for the design, architecture, and implementation of applications and tools focused on the next generation of software development.&amp;nbsp; Over the years I've accumulated a small but useful library which I call "stuff that should have been in System.dll".&amp;nbsp; For example I've defined simple things like &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;&amp;lt;T1, T2&amp;gt;&lt;/FONT&gt; through &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Tuple&lt;/SPAN&gt;&amp;lt;T1, ..., T9&amp;gt;&lt;/FONT&gt;, utility things like a generic &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Cache&lt;/SPAN&gt;&amp;lt;...&amp;gt;&lt;/FONT&gt; that uses weak-references, and more complex things like a &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ThreadQueue&lt;/SPAN&gt;&lt;/FONT&gt; which schedules threads on the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ThreadPool&lt;/SPAN&gt;&lt;/FONT&gt; that can't run in parallel or out of order.&amp;nbsp; None of these things are rocket science but they prove to be useful time and time again.&amp;nbsp; I'll try to fill the gap in my blog by posting some of these and pre-dating them to match the date they were created.&amp;nbsp; Thanks for reading!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9000225" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Personal/default.aspx">Personal</category></item><item><title>Presentation.More</title><link>http://blogs.msdn.com/kaelr/archive/2008/05/21/presentation-more.aspx</link><pubDate>Thu, 22 May 2008 01:18:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9013921</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9013921.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9013921</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9013921</wfw:comment><description>&lt;p&gt;In the same vein as &lt;a href="http://blogs.msdn.com/kaelr/archive/2008/05/12/system-more.aspx"&gt;System.More&lt;/a&gt;, I've also created a Windows Presentation Foundation counterpart called Presentation.More.&amp;#160; And just like System.More defines classes in the same System namespaces as System.dll and System.Core.dll, Presentation.More also defines classes in the System.Windows namespaces directly.&amp;#160; Not only does this work elegantly for code-based languages like C# and VB, but it also works in XAML by simply adding &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.markup.xmlnsdefinitionattribute.aspx"&gt;XmlnsDefinitionAttribute&lt;/a&gt;&lt;/span&gt;s&lt;/font&gt; to the Presentation.More assembly.&amp;#160; So far I have classes in six different namespaces under System.Windows, so I have a file called XmlnsDefinitions.cs in my Presentation.More project that looks like this:&lt;/p&gt;  &lt;pre style="word-wrap: break-word; white-space: pre-wrap"&gt;&lt;font size="1" face="Consolas, Courier"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Windows.Markup;

[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows.Controls&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows.Data&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows.Documents&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows.Input&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: blue"&gt;assembly&lt;/span&gt;: &lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;System.Windows.Media&amp;quot;&lt;/span&gt;)]&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;For example, one of the classes is a &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;NineGridImage&lt;/span&gt;&lt;/font&gt; which lives in &lt;font face="Consolas, Courier"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;System.Windows.Controls&lt;/font&gt;.&amp;#160; Without &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;XmlnsDefinitionAttribute&lt;/span&gt;s&lt;/font&gt;, your application's XAML would have to declare the namespace and use it everywhere you wanted to use the control:&lt;/p&gt;

&lt;pre style="word-wrap: break-word; white-space: pre-wrap"&gt;&lt;font face="Consolas, Courier"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Window &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;WpfApplication1.Window1&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
        &lt;/span&gt;&lt;span style="background: yellow"&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;more&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:System.Windows.Controls;assembly=Presentation.More&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="background: yellow"&gt;&lt;span style="color: #a31515"&gt;more&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;NineGridImage &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;...&amp;quot;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Window&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;But by adding the &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;XmlnsDefinition&lt;/span&gt;&lt;/font&gt; attribute to the Presentation.More assembly for the &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #a31515"&gt;System.Windows.Controls&lt;/span&gt;&lt;/font&gt; namespace above you can use a &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #a31515"&gt;NineGridImage&lt;/span&gt;&lt;/font&gt; just like you'd use any other built-in control:&lt;/p&gt;

&lt;pre style="word-wrap: break-word; white-space: pre-wrap"&gt;&lt;font face="Consolas, Courier"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Window &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;WpfApplication1.Window1&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;NineGridImage &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;...&amp;quot;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;Window&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;

&lt;p&gt;Some may think this is confusing or even unethical, but I think it makes the resulting application code clearer and more elegant.&amp;#160; I don't suggest overriding the System namespaces for &lt;em&gt;every&lt;/em&gt; class, especially when a class is specific to a particular application.&amp;#160; But for classes that have nothing to do with a particular client application and &amp;quot;should have been in System.dll&amp;quot; or &amp;quot;should have been in PresentationFramework.dll&amp;quot;, I think it's best to treat them as part of the framework rather than bring attention to yet another namespace.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9013921" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Presentation.More/default.aspx">Presentation.More</category></item><item><title>System.More</title><link>http://blogs.msdn.com/kaelr/archive/2008/05/12/system-more.aspx</link><pubDate>Tue, 13 May 2008 00:51:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9013851</guid><dc:creator>kaelr</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9013851.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9013851</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9013851</wfw:comment><description>&lt;p&gt;I, like most developers out there, have accumulated a set of classes and utility functions that have been used repeatedly in multiple projects throughout my career.&amp;#160; This code has followed me around in various forms; sometimes by copying the files directly to new projects, and other times by creating a shared DLL that's referenced as a dependency instead.&amp;#160; Whether the files were copied in their own folder or compiled into a DLL, the question always remained of &amp;quot;what should these be called&amp;quot;?&amp;#160; They've gone by many names, such as &amp;quot;common&amp;quot;, &amp;quot;shared&amp;quot;, &amp;quot;utilities&amp;quot; and even just &amp;quot;utils&amp;quot;.&amp;#160; I've never been satisfied with any of these, and they don't convey the meaning of what I like to call &amp;quot;stuff that should have been in System.dll&amp;quot;.&amp;#160; One day, after looking through the .NET base class library, I noticed that some of the most basic types in the System namespace (like &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&lt;/font&gt; and &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&lt;/font&gt;) are not in System.dll or mscorlib, but are actually defined in System.Core.dll.&amp;#160; So in the same vein I decided to name my library &amp;quot;System.More&amp;quot; with all of the same expectations thereof.&amp;#160; I stripped out all of the &amp;quot;utility&amp;quot;-like classes and methods, including implementations of algorithms such as &lt;a href="http://en.wikipedia.org/wiki/Traveling_Salesman_Problem"&gt;traveling salesman&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Huffman_coding"&gt;huffman coding&lt;/a&gt;, because I couldn't see those belonging in System.dll.&amp;#160; Instead, System.More contains classes like &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;Tuple&lt;/span&gt;&lt;/font&gt; and &lt;font face="Consolas, Courier"&gt;&lt;span style="color: #2b91af"&gt;PriorityQueue&lt;/span&gt;&lt;/font&gt; that are defined in &lt;font face="Consolas, Courier"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;System&lt;/font&gt; and &lt;font face="Consolas, Courier"&gt;System.Collections.Generic&lt;/font&gt; respectively.&amp;#160; This way, new projects can simply add a reference to System.More.dll (just like adding one to System.Core.dll) to get all of the new classes and extensions easily and elegantly.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9013851" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Personal/default.aspx">Personal</category><category domain="http://blogs.msdn.com/kaelr/archive/tags/System.More/default.aspx">System.More</category></item><item><title>Comparator</title><link>http://blogs.msdn.com/kaelr/archive/2007/10/04/comparator.aspx</link><pubDate>Fri, 05 Oct 2007 02:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9013768</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9013768.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9013768</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9013768</wfw:comment><description>&lt;P&gt;I recently wrote a generic &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;A href="http://en.wikipedia.org/wiki/Priority_queue" mce_href="http://en.wikipedia.org/wiki/Priority_queue"&gt;PriorityQueue&lt;/A&gt;&lt;/SPAN&gt;&lt;/FONT&gt; class which can treat any type of object as the priority (not just integers).&amp;nbsp; Specifically, the class looked like this:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;PriorityQueue&lt;/SPAN&gt;&amp;lt;T, TPriority&amp;gt;
{
    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;PriorityQueue()
        : &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;)
    {
        ...
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;PriorityQueue(&lt;SPAN style="COLOR: blue"&gt;bool &lt;/SPAN&gt;invert)
        : &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Comparer&lt;/SPAN&gt;&amp;lt;TPriority&amp;gt;.Default)
    {
        ...
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;PriorityQueue(&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&amp;lt;TPriority&amp;gt; comparer)
    {
        ...
    }

    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;Enqueue(T item, TPriority priority)
    {
        ...
    }

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;KeyValuePair&lt;/SPAN&gt;&amp;lt;T, TPriority&amp;gt; Dequeue()
    {
        ...
    }

    ...
}
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Since it allowed any type of object as the priority object, it obviously can't use the standard numerical &lt;FONT face="Consolas, Courier"&gt;&amp;lt;&lt;/FONT&gt; and &lt;FONT face="Consolas, Courier"&gt;&amp;gt;&lt;/FONT&gt; operators to compare them.&amp;nbsp; Many classes in the .NET framework (such as &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" mce_href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx"&gt;List&amp;lt;T&amp;gt;&lt;/A&gt;&lt;/FONT&gt;) have this same pattern, so the framework provides an &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx" mce_href="http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx"&gt;IComparer&amp;lt;T&amp;gt;&lt;/A&gt;&lt;/FONT&gt; interface and a &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/cfttsh47.aspx" mce_href="http://msdn.microsoft.com/en-us/library/cfttsh47.aspx"&gt;Comparer&amp;lt;T&amp;gt;&lt;/A&gt;&lt;/FONT&gt; base class to support comparison of arbitrary types.&amp;nbsp; The interface only has one method you have to implement: &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;Compare(T x, T y)&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;Every time I implement &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&lt;/FONT&gt; I think it's silly that I have to create a new class (which usually also means a new file) that just defines one method.&amp;nbsp; In fact, the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;Compare&lt;/FONT&gt; method is usually just a one-liner like &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Compare(x.SomeProperty, y.SomeProperty)&lt;/FONT&gt; since all I want to do is sort items based on &lt;FONT face="Consolas, Courier"&gt;SomeProperty&lt;/FONT&gt;.&amp;nbsp; Why do I have to create a whole new class (and whole new file) just to specify &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Compare(x.SomeProperty, y.SomeProperty)&lt;/FONT&gt;?&lt;/P&gt;
&lt;P&gt;Obviously someone in the .NET framework team felt the same way, so they introduced a &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/tfakywbh.aspx" mce_href="http://msdn.microsoft.com/en-us/library/tfakywbh.aspx"&gt;Comparison&amp;lt;T&amp;gt;&lt;/A&gt;&lt;/FONT&gt; delegate and added overloads to &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/cxt053xf.aspx" mce_href="http://msdn.microsoft.com/en-us/library/cxt053xf.aspx"&gt;Array.Sort&amp;lt;T&amp;gt;&lt;/A&gt;&lt;/FONT&gt; and &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx" mce_href="http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx"&gt;List&amp;lt;T&amp;gt;.Sort&lt;/A&gt;&lt;/FONT&gt; which accept this delegate instead of requiring an implementation of the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&lt;/FONT&gt; interface.&amp;nbsp; Unfortunately they only added overloads for those two methods; there are still over twenty other methods in the .NET framework that require the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt; interface, including LINQ's &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt; extension methods.&lt;/P&gt;
&lt;P&gt;In order to use the single-delegate idea in all of the places that still require the interface, I created a new class called &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Comparator&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt;:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Comparator&lt;/SPAN&gt;&amp;lt;T&amp;gt; : &lt;SPAN style="COLOR: #2b91af"&gt;Comparer&lt;/SPAN&gt;&amp;lt;T&amp;gt;
{
    &lt;SPAN style="COLOR: blue"&gt;private readonly &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Comparison&lt;/SPAN&gt;&amp;lt;T&amp;gt; comparison;

    &lt;SPAN style="COLOR: blue"&gt;public &lt;/SPAN&gt;Comparator(&lt;SPAN style="COLOR: #2b91af"&gt;Comparison&lt;/SPAN&gt;&amp;lt;T&amp;gt; comparison)
    {
        &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.comparison = comparison;
    }

    &lt;SPAN style="COLOR: blue"&gt;public override int &lt;/SPAN&gt;Compare(T x, T y)
    {
        &lt;SPAN style="COLOR: blue"&gt;return this&lt;/SPAN&gt;.comparison(x, y);
    }
}
&lt;/FONT&gt;&lt;/PRE&gt;&lt;FONT size=1&gt;[Comments and argument checking omitted for brevity.]&lt;/FONT&gt; 
&lt;P&gt;This is probably the simplest class in my library.&amp;nbsp; It just takes a &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Comparison&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt; delegate in the constructor and implements &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Comparer&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt; (which also implements &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&lt;/FONT&gt; and &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;/FONT&gt;) by simply calling the comparison function.&amp;nbsp; This tiny little class allows you to use all of the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IComparer&lt;/SPAN&gt;&lt;/FONT&gt;-based methods and constructors (such as &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SortedList&lt;/SPAN&gt;(comparer)&lt;/FONT&gt;, &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SortedDictionary&lt;/SPAN&gt;(comparer)&lt;/FONT&gt;, &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;T&amp;gt;.OrderBy()&lt;/FONT&gt;, etc) without having to create a new class every time.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9013768" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/kaelr/attachment/9013768.ashx" length="1898" type="text/plain" /><category domain="http://blogs.msdn.com/kaelr/archive/tags/System.More/default.aspx">System.More</category></item><item><title>SynchronizationCallback</title><link>http://blogs.msdn.com/kaelr/archive/2007/09/05/synchronizationcallback.aspx</link><pubDate>Thu, 06 Sep 2007 03:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9251404</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9251404.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9251404</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9251404</wfw:comment><description>&lt;P&gt;It's common for user interface frameworks (such as &lt;A href="http://msdn.microsoft.com/en-us/netframework/aa497342.aspx" mce_href="http://msdn.microsoft.com/en-us/netframework/aa497342.aspx"&gt;Windows Forms&lt;/A&gt; applications or &lt;A href="http://msdn.microsoft.com/en-us/library/bb286983.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb286983.aspx"&gt;Visual Studio&lt;/A&gt; packages) to restrict API calls to a single thread; specifically the "UI" thread that is usually the original or "main" thread in the process.&amp;nbsp; This means that if you spawn a background thread then it can't touch the user interface's objects and must use some other synchronization mechanism to touch the user interface on the main thread instead.&amp;nbsp; For example in the case of WinForms you can call &lt;A href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx" mce_href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx"&gt;Invoke&lt;/A&gt; or &lt;A href="http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx" mce_href="http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx"&gt;BeginInvoke&lt;/A&gt; if you have a reference to a &lt;A href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx"&gt;Control&lt;/A&gt;.&amp;nbsp; This usually isn't too much of a burden for most application code since it can easily get access to a &lt;A href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx"&gt;Control&lt;/A&gt; instance, but it makes it harder for reusable library code to offload things to a background thread in a generic way since it may not have a convenient way to get "back" to the main thread.&amp;nbsp; It might seem like library code wouldn't need to worry about this since it usually doesn't touch the user interface objects, but the problem comes back because of event handlers.&amp;nbsp; Often the user interface code will attach event handlers to the reusable library's objects, and if those events are raised from a background thread then the event handler will also be executing on the background thread and will be prone to touching the user interface inappropriately.&amp;nbsp; This can be guarded against by always having every event handler call &lt;A href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx" mce_href="http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx"&gt;Invoke&lt;/A&gt;, but that's a huge amount of code overhead just to protect against something that the library &lt;EM&gt;might&lt;/EM&gt; do.&lt;/P&gt;
&lt;P&gt;Fortunately, there's a generic class called &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx"&gt;SynchronizationContext&lt;/A&gt; with methods &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx"&gt;Send&lt;/A&gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.post.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.post.aspx"&gt;Post&lt;/A&gt; (which are analogous to Invoke and BeginInvoke) that is always accessible via the static &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.current.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.current.aspx"&gt;SynchronizationContext.Current&lt;/A&gt; property.&amp;nbsp; User interface frameworks (like WinForms) override the current &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.aspx"&gt;SynchronizationContext&lt;/A&gt; so that the &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx"&gt;Send&lt;/A&gt; and &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.post.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.post.aspx"&gt;Post&lt;/A&gt; simply call the appropriate Invoke and BeginInvoke methods respectively.&amp;nbsp; This means that if your library code always uses &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx"&gt;Send&lt;/A&gt; when raising events from its background thread(s) then user interface code will be safe by default.&amp;nbsp; However, there's a catch: &lt;A href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.current.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.current.aspx"&gt;SynchronizationContext.Current&lt;/A&gt; is only set in the &lt;EM&gt;main&lt;/EM&gt; thread (which is the only thread where you don't actually need it), as shown with the following code:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyLibraryClass
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;CalledInMainThread()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Debug&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Main Thread: SynchronizationContext.Current is " &lt;/SPAN&gt;+ (&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current != &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;? &lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current.ToString() : &lt;SPAN style="COLOR: #a31515"&gt;"null"&lt;/SPAN&gt;));

        &lt;SPAN style="COLOR: #2b91af"&gt;Thread &lt;/SPAN&gt;t = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ThreadStart&lt;/SPAN&gt;(CalledInBackgroundThread));
        t.Start();
    }

    &lt;SPAN style="COLOR: blue"&gt;private void &lt;/SPAN&gt;CalledInBackgroundThread()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Debug&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Background Thread: SynchronizationContext.Current is " &lt;/SPAN&gt;+ (&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current != &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;? &lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current.ToString() : &lt;SPAN style="COLOR: #a31515"&gt;"null"&lt;/SPAN&gt;));
    }
}
&lt;/FONT&gt;&lt;/PRE&gt;&lt;BR&gt;
&lt;P&gt;Prints output:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;Main Thread: SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext

Background Thread: SynchronizationContext.Current is null
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;The obvious way to work around this is to grab the reference to &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; in the main thread and then make it available to the background thread by either storing it in a shared field or by passing it as the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;state&lt;/FONT&gt; argument to those callbacks that support it.&amp;nbsp; The drawback with this method is that the code running in the background needs to keep passing the reference around to everyone who needs it, which kind of defeats the purpose of the static global &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; property.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Fortunately there is a &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.SetSynchronizationContext&lt;/FONT&gt; method that sets the value of the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; property, so if the first method in the background thread always calls SetSynchronizationContext with the instance&amp;nbsp;from the main thread&amp;nbsp;then all of the other code in the background thread can use it without having to pass the reference around.&amp;nbsp; This also makes it so you can write your code without having to care whether it's going to be called on the main thread or on a background thread since you can assume &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; will always be set appropriately.&lt;/P&gt;
&lt;P&gt;I use this pattern quite often, so I created a new type of delegate that handles marshalling the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; property from the main thread to the background thread automatically.&amp;nbsp; I call this delegate type &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationCallback&lt;/SPAN&gt;&lt;/FONT&gt;, and it's compatible with all of the other delegate types such as &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;WaitCallback&lt;/SPAN&gt;&lt;/FONT&gt;, &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TimerCallback&lt;/SPAN&gt;&lt;/FONT&gt;, &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ThreadStart&lt;/SPAN&gt;&lt;/FONT&gt;, etc.&amp;nbsp; By simply using this delegate type instead of the original type you'll be able to assume &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current&lt;/FONT&gt; will always be valid no matter which thread your code is running on.&amp;nbsp; For example, by changing the above code as follows:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;public class &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MyLibraryClass
&lt;/SPAN&gt;{
    &lt;SPAN style="COLOR: blue"&gt;public void &lt;/SPAN&gt;CalledInMainThread()
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Debug&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Main Thread: SynchronizationContext.Current is " &lt;/SPAN&gt;+ (&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current != &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;? &lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current.ToString() : &lt;SPAN style="COLOR: #a31515"&gt;"null"&lt;/SPAN&gt;));

        &lt;SPAN style="COLOR: #2b91af"&gt;Thread &lt;/SPAN&gt;t = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;(&lt;SPAN style="BACKGROUND: yellow"&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;ThreadStart&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationCallback&lt;/SPAN&gt;&lt;/SPAN&gt;(CalledInBackgroundThread));
        t.Start();
    }

    &lt;SPAN style="COLOR: blue"&gt;private void &lt;/SPAN&gt;CalledInBackgroundThread(&lt;SPAN style="COLOR: blue"&gt;object &lt;/SPAN&gt;state)
    {
        &lt;SPAN style="COLOR: #2b91af"&gt;Debug&lt;/SPAN&gt;.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Background Thread: SynchronizationContext.Current is " &lt;/SPAN&gt;+ (&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current != &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;? &lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;.Current.ToString() : &lt;SPAN style="COLOR: #a31515"&gt;"null"&lt;/SPAN&gt;));
    }
}
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR&gt;Then you'll see the output as expected:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;Main Thread: SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext

Background Thread: SynchronizationContext.Current is System.Windows.Forms.WindowsFormsSynchronizationContext
&lt;/FONT&gt;&lt;/PRE&gt;&lt;BR&gt;I've attached the full &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SynchronizationContext&lt;/SPAN&gt;&lt;/FONT&gt; class for your convenience.&amp;nbsp; Enjoy!&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9251404" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/kaelr/attachment/9251404.ashx" length="17030" type="text/plain" /><category domain="http://blogs.msdn.com/kaelr/archive/tags/System.More/default.aspx">System.More</category></item><item><title>IndentedTextWriter</title><link>http://blogs.msdn.com/kaelr/archive/2006/03/28/indentedtextwriter.aspx</link><pubDate>Tue, 28 Mar 2006 22:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9245744</guid><dc:creator>kaelr</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/9245744.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=9245744</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=9245744</wfw:comment><description>&lt;P&gt;There's a very useful class called &lt;A href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter.aspx"&gt;IndentedTextWriter&lt;/A&gt; in the &lt;A href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.aspx"&gt;System.CodeDom.Compiler&lt;/A&gt; namespace that automatically adds whitespace before each line depending on its &lt;A href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter.indent.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.codedom.compiler.indentedtextwriter.indent.aspx"&gt;Indent&lt;/A&gt; property.&amp;nbsp; This works great when you have code with several nested (or recursive) subroutines and you want them to print nested output but you don't want to keep track of how "nested" they are by yourself.&amp;nbsp; Unfortunately, there's a bug in the framework's implementation that prevents the first line from being indented.&amp;nbsp; To illustrate, take this program:&lt;/P&gt;&lt;PRE style="WORD-WRAP: break-word"&gt;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;static void &lt;/SPAN&gt;Main(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] args)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;IndentedTextWriter &lt;/SPAN&gt;writer = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IndentedTextWriter&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.Out);
    writer.Indent = 1;
    writer.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"First line"&lt;/SPAN&gt;);
    writer.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Second line"&lt;/SPAN&gt;);
    writer.WriteLine(&lt;SPAN style="COLOR: #a31515"&gt;"Third line"&lt;/SPAN&gt;);
}
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;You'll notice that the first line&amp;nbsp;isn't indented:&lt;BR&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=131 alt=image src="http://blogs.msdn.com/blogfiles/kaelr/WindowsLiveWriter/Func_830D/image_18.png" width=545 border=0 mce_src="http://blogs.msdn.com/blogfiles/kaelr/WindowsLiveWriter/Func_830D/image_18.png"&gt; &lt;/P&gt;
&lt;P&gt;The bug is easy to fix with a little reflection.&amp;nbsp; Before the first&amp;nbsp;&lt;FONT face=Consolas&gt;Write&lt;/FONT&gt; or&amp;nbsp;&lt;FONT face=Consolas&gt;WriteLine&lt;/FONT&gt; simply call&amp;nbsp;&lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(System.CodeDom.Compiler.&lt;SPAN style="COLOR: #2b91af"&gt;IndentedTextWriter&lt;/SPAN&gt;).GetField(&lt;SPAN style="COLOR: #a31515"&gt;"tabsPending"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;BindingFlags&lt;/SPAN&gt;.Instance | &lt;SPAN style="COLOR: #2b91af"&gt;BindingFlags&lt;/SPAN&gt;.NonPublic).SetValue(&lt;SPAN style="COLOR: blue"&gt;&lt;FONT color=#000000&gt;writer&lt;/FONT&gt;&lt;/SPAN&gt;, &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;);&lt;/FONT&gt; &lt;/P&gt;
&lt;P&gt;I've attached a simple wrapper class that applies the fix automatically and moves the &lt;FONT face="Consolas, Courier"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;IndentedTextWriter&lt;/SPAN&gt;&lt;/FONT&gt; class to the more common &lt;FONT face="Consolas, Courier"&gt;System.IO&lt;/FONT&gt; namespace at the same time.&amp;nbsp; Enjoy!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9245744" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/kaelr/attachment/9245744.ashx" length="3527" type="text/plain" /><category domain="http://blogs.msdn.com/kaelr/archive/tags/System.More/default.aspx">System.More</category></item><item><title>Hello, World!</title><link>http://blogs.msdn.com/kaelr/archive/2000/05/15/hello-world.aspx</link><pubDate>Mon, 15 May 2000 18:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:184412</guid><dc:creator>kaelr</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/kaelr/comments/184412.aspx</comments><wfw:commentRss>http://blogs.msdn.com/kaelr/commentrss.aspx?PostID=184412</wfw:commentRss><wfw:comment>http://blogs.msdn.com/kaelr/rsscomments.aspx?PostID=184412</wfw:comment><description>My name is Kael Rowan. I am a Software Design Engineer at Microsoft, and this is my first blog.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=184412" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/kaelr/archive/tags/Personal/default.aspx">Personal</category></item></channel></rss>