<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Code Blitz</title>
	<atom:link href="http://codeblitz.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeblitz.wordpress.com</link>
	<description>Separation of Concerns</description>
	<lastBuildDate>Tue, 22 Nov 2011 08:20:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='codeblitz.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/149993605d87bd97a988e1c61f755c2a?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Code Blitz</title>
		<link>http://codeblitz.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://codeblitz.wordpress.com/osd.xml" title="Code Blitz" />
	<atom:link rel='hub' href='http://codeblitz.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Enable Software Rendering in WPF programmatically</title>
		<link>http://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf-programmatically/</link>
		<comments>http://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf-programmatically/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 22:11:36 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1096</guid>
		<description><![CDATA[From my previous post, I talked about troubleshooting WPF graphic issues. One of those options is to set a registry key to enable software rendering for WPF on that machine only. But that can be an intrusive setting and may not suit everyone&#8217;s needs. There is a another option, which is to enable software rendering in code. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1096&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>From <a href="http://codeblitz.wordpress.com/2010/09/13/graphic-issues-with-wpf/" target="_blank">my previous post</a>, I talked about troubleshooting WPF graphic issues. One of those options is to set a registry key to enable software rendering for WPF on that machine only. But that can be an intrusive setting and may not suit everyone&#8217;s needs.</p>
<p>There is a another option, which is to enable software rendering in code. If you are working on .NET 3.5 framework, there&#8217;s a way to set that up..unfortunately you have to set that up per window or per target control. But might be useful for those that want to selectively tweak rendering mode for each window.</p>
<p><pre class="brush: csharp;">
private void ConfigureSoftwareRendering()
{
    if (shouldEnableSoftwareRendering)
    {
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        HwndTarget hwndTarget = hwndSource.CompositionTarget;
        hwndTarget.RenderMode = RenderMode.SoftwarewareOnly;
    }
}
</pre></p>
<p>If you&#8217;re developing in .NET 4 framework, then you have another option, which is to set up software rendering per process&#8230;.like so.</p>
<p><pre class="brush: csharp;">
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
         if (shouldEnableSoftwareRendering)
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
    }
}
</pre></p>
<p>You might be wondering why we should use software rendering for WPF applications. It depends on the end-user hardware configuration which might have &#8220;WPF-unfriendly&#8221; drivers, or just no graphics card. In some situations <a href="http://blogs.msdn.com/b/seema/archive/2006/10/25/layered-windows-sw-is-sometimes-faster-than-hw.aspx" target="_blank">software rendering might be faster than hardware rendering</a> &#8230; hope this helps.</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=Ehttp://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf/&amp;title=Enable Software Rendering in WPF programmatically" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1096/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1096/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1096/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1096&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/09/15/enable-software-rendering-in-wpf-programmatically/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>Graphic issues with WPF</title>
		<link>http://codeblitz.wordpress.com/2010/09/13/graphic-issues-with-wpf/</link>
		<comments>http://codeblitz.wordpress.com/2010/09/13/graphic-issues-with-wpf/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 22:14:48 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1092</guid>
		<description><![CDATA[Graphic issues with WPF are not uncommon at all. This may be partially due to graphic card drivers having varying effects with hardware rendering on WPF applications . The same application may look different on PCs with different hardware specifications. In our situation, we had an image rendering with jagged lines on a couple of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1092&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Graphic issues with WPF are not uncommon at all. This may be partially due to graphic card drivers having varying effects with hardware rendering on WPF applications . The same application may look different on PCs with different hardware specifications. In our situation, we had an image rendering with jagged lines on a couple of PCs, and looked fine on others.</p>
<p>In order to troubleshoot if it&#8217;s a hardware problem, or your code, I find that the simplest approach is t<a href="http://msdn.microsoft.com/en-us/library/aa970912.aspx" target="_blank">o first disable hardware rendering on your machine, by modifying a registry key</a>. That registry key might not be on your machine, so add it if it isn&#8217;t.  By default if that key is absent, WPF assumes hardware rendering should be enabled.</p>
<p>After you reboot, and the graphic problem has disappeared because your PC is now using software rendering, you know for sure that your code is fine. Next you will have to update your graphics card driver, computer bios, and make sure you have at least .NET Framework 3.5 SP1 installed.</p>
<p>Here&#8217;s a link for<a href="http://support.microsoft.com/kb/963021" target="_blank"> Guidelines for troubleshooting graphic issues on WPF</a>. It&#8217;s quite useful, and covers what I discussed above. Give this a go, it has helped identified our graphics problem very swiftly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1092/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1092/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1092/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1092&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/09/13/graphic-issues-with-wpf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>
	</item>
		<item>
		<title>Declarative state of mind</title>
		<link>http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/</link>
		<comments>http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 11:44:41 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[declarative]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1073</guid>
		<description><![CDATA[Over the years, we have seen C# language evolve into a more dynamic language, and also have seen numerous frameworks and APIs out there implementing a more declarative style. Famous example would be LINQ, and one can appreciate how the language flows and how readable it is. For too many years we have programmed in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1073&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Over the years, we have seen C# language evolve into a more dynamic language, and also have seen numerous frameworks and APIs out there implementing a more declarative style. Famous example would be LINQ, and one can appreciate how the language flows and how readable it is. For too many years we have programmed in a procedural manner, and we have forgotten who the target audience for our code is&#8230;..yes the audience is us, the programmers!!!!</p>
<p>Procedural code can be hard to read, which makes code hard to maintain. To be declarative is to write code in a way that declares your intention fluidly using <a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_self">fluent interfaces</a>. I&#8217;ve come up with an example that illustrates this.</p>
<p><span id="more-1073"></span></p>
<p>In methods, it&#8217;s very common to write guard conditions against method parameters, be it value types or reference types. An example below illustrates this.</p>
<p><pre class="brush: csharp;">
public double Divide(int top, int bottom)
{
    if (bottom == 0)
    {
        throw new MyBusinessException(&quot;Cannot divide by zero&quot;);
    }
    return top / bottom;
}
</pre></p>
<blockquote><p><em>You may be thinking, why don&#8217;t we just let .NET throw a DivideByZeroException. I tend to disagree with this approach, because I feel that it is much more appropriate for an application to throw and handle it&#8217;s own category of exceptions&#8230;.but that in itself is another can of worms. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </em></p></blockquote>
<p>If you have lots of parameters, and you have to guard against them, and these checks get pretty large very quickly, which makes it hard to read. Of course if you&#8217;re developing in .NET 4, there&#8217;s <a href="http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx" target="_blank">Code Contracts</a>, but if you&#8217;re not, here&#8217;s my version of making it declarative.</p>
<p><pre class="brush: csharp;">
public double Divide(int top, int bottom)
{
    Verify.ThatValue(bottom).WhenEqualsTo(0).WillThrowException&lt;MyBusinessException&gt;(&quot;Cannot divide by zero&quot;);
    return top / bottom;
}
</pre></p>
<p>I&#8217;ve created a Verify library for this purpose, and it understands reference and value types. The fluency with the method chaining allows one to easily understand what that line of code is doing, somewhat like reading a sentence&#8230;.Verify that the value &#8216;bottom&#8217;, when equals to zero will throw a MyBusinessException with message &#8220;Cannot divide by zero&#8221;.</p>
<p>Here&#8217;s another example. When someClass is null, we write a warning to Trace, and throw an exception.</p>
<p><pre class="brush: csharp;">
public void SomeMethod(SomeClass someClass)
{
    Verify.ThatClass(someClass).WhenIsNull()
        .WillExecuteAction(() =&gt; Trace.TraceWarning(&quot;some class is null&quot;))
        .WillThrowException&lt;MyBusinessException&gt;(&quot;some class is null&quot;);
    // do something here.
}
</pre></p>
<p>There are quite a number of useful checks, and here&#8217;s the interface for Verify for reference type.</p>
<p><pre class="brush: csharp;">
public interface IVerifyClassCondition&lt;T&gt; where T : class
{
    IVerifyExceptionThrower WhenIsNull();

    IVerifyExceptionThrower WhenIsNotNull();

    IVerifyExceptionThrower WhenEqualTo(T obj);

    IVerifyExceptionThrower WhenNotEqualTo(T obj);

    IVerifyExceptionThrower WhenMeetsCondition(Func&lt;T, bool&gt; condition);
}
</pre></p>
<p>Here&#8217;s the interface for Verify for value types.</p>
<p><pre class="brush: csharp;">
public interface IVerifyStructCondition&lt;T&gt;  where T : struct, IComparable&lt;T&gt;, IEquatable&lt;T&gt;
{

    IVerifyExceptionThrower WhenGreaterThan(T obj);

    IVerifyExceptionThrower WhenLessThan(T obj);

    IVerifyExceptionThrower WhenEqualsTo(T obj);

    IVerifyExceptionThrower WhenNotEqualsTo(T obj);

    IVerifyExceptionThrower WhenGreaterThanEqualTo(T obj);

    IVerifyExceptionThrower WhenLessThanEqualTo(T obj);

    IVerifyExceptionThrower WhenIsDefault();

    IVerifyExceptionThrower WhenIsNotDefault();

    IVerifyExceptionThrower WhenMeetsCondition(Func&lt;T, bool&gt; condition);
}
</pre></p>
<p>I guess you get the idea. It&#8217;s simple and straightforward, but how often do we bother to write fluent interfaces like this&#8230;..I hope this has helped you gain a new appreciation for declarative style and it&#8217;s readability. You can download the sample solution below, and I&#8217;ve included a whole bunch of unit tests.</p>
<p><a href="http://cid-2c6600f1c1d5e3be.office.live.com/self.aspx/.Public/Verify%20Library%20VS2010.zip" target="_blank">Download this library for VS 2010 here</a>.</p>
<p><a href="http://cid-2c6600f1c1d5e3be.office.live.com/self.aspx/.Public/Verify%20Library%20VS2008.zip" target="_blank">Download this library for VS 2008 here</a>. (I modified the csproj and solution files from vs2010, hope it works)</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/&amp;title=Declarative state of mind" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1073/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1073&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/09/02/declarative-state-of-mind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>Parallel MSBuild</title>
		<link>http://codeblitz.wordpress.com/2010/08/29/parallel-msbuild/</link>
		<comments>http://codeblitz.wordpress.com/2010/08/29/parallel-msbuild/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 22:21:18 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Parallelism]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1068</guid>
		<description><![CDATA[Did you know MSBuild can now run your build using all the cores on your proccessor? So if your development machine has multi-cores, and your solution is fairly large (perhaps more than 15 projects, but it varies), and you&#8217;re developing in .NET Framework 3.5 or higher, you should give parallel builds a go, and see [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1068&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Did you know MSBuild can now run your build using all the cores on your proccessor? So if your development machine has multi-cores, and your solution is fairly large (perhaps more than 15 projects, but it varies), and you&#8217;re developing in .NET Framework 3.5 or higher, you should give parallel builds a go, and see if there&#8217;s any savings in compile time.</p>
<p>Scott Hanselman has 2 great articles on this. He explains <a href="http://www.hanselman.com/blog/HackParallelMSBuildsFromWithinTheVisualStudioIDE.aspx" target="_blank">how to incorporate parallel builds into Visual Studio</a>, and also talks about <a href="http://www.hanselman.com/blog/FasterBuildsWithMSBuildUsingParallelBuildsAndMulticoreCPUs.aspx" target="_blank">how to run parallel build in your build process and scripts</a>.</p>
<p>Take note that it&#8217;s not a guarantee that it is always going to make your solution build faster, it also depends on your machine specifications as well as how your project dependency structure. If that structures is linear rather than a tree structure, there would not be much benefit running parallel builds. Also take care about how your project references are used, because running parallel build might cause some unexpected locking problems.</p>
<p>On a more positive note, I&#8217;m working on a solution with around 57 projects, and using an 8 core PC. Running parallel build, it shaved off 25 seconds off the usual 2 minutes build. For argument&#8217;s sake, if I build my solution say 50 times a time (which is a very optimistic value in my opinion),  I save about  20 minutes a day from compiling (assuming you have an 8 hour work day). That&#8217;s a big savings, and that benefit grows over the weeks and months for every developer on your team. Don&#8217;t forget your build scripts and automated builds too!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1068/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1068/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1068/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1068&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/08/29/parallel-msbuild/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>
	</item>
		<item>
		<title>ResourceDictionary: Use with care</title>
		<link>http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/</link>
		<comments>http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 12:06:16 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1046</guid>
		<description><![CDATA[Seems like WPF is plagued with memory leaks and problems, if not handled with care. Resource dictionary is another potential pain point, the current project I&#8217;m working on is one such victim. Consider an UserControl that references a ResourceDictionary in it&#8217;s MergedDictionaries. Consider that the referenced ResourceDictionary references other ResourceDictionaries, and so on. Now consider [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1046&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Seems like WPF is plagued with memory leaks and problems, if not handled with care. Resource dictionary is another potential pain point, the current project I&#8217;m working on is one such victim.</p>
<p>Consider an UserControl that references a ResourceDictionary in it&#8217;s MergedDictionaries. Consider that the referenced ResourceDictionary references other ResourceDictionaries, and so on. Now consider that you render 30 of these user controls on a window, each user control creates a unique instance of the same ResourceDictionary, plus much more. Now you suddenly have heaps of memory being allocated, unnecessary memory for that matter. Apparently this occurs in .NET 3.5 only, not 4.0.</p>
<p><span id="more-1046"></span></p>
<p>In order to resolve this, the simple resolution I can think of is to reference all the Resource Dictionaries you need in your App.xaml, and remove the user control&#8217;s Resource Dictionaries. However it might not always be possible, so I had to look for another approach.</p>
<p>Fortunately I came across <a href="http://www.wpftutorial.net/MergedDictionaryPerformance.html" target="_blank">an excellent article that has a nice solution for this problem by Christian Mosers</a>.</p>
<p>I added a tweak to that solution, which is to use a <a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" target="_blank">WeakReference</a>. In theory, this should allow the controls referencing the cached Resource Dictionaries to be garbage collected since we are using Weak References. I have not had the opportunity to verify this yet, so if you do please let me know.</p>
<p><pre class="brush: csharp;">
public class CachedResourceDictionary : ResourceDictionary
{
    private static Dictionary&lt;Uri, WeakReference&gt; _cache;

    static CachedResourceDictionary()
    {
        _cache = new Dictionary&lt;Uri, WeakReference&gt;();
    }

    private Uri _source;

    public new Uri Source
    {
        get { return _source; }
        set
        {
            _source = value;
            if (!_cache.ContainsKey(_source))
            {
                AddToCache();
            }
            else
            {
                WeakReference weakReference = _cache[_source];
                if (weakReference != null &amp;&amp; weakReference.IsAlive)
                {
                    MergedDictionaries.Add((ResourceDictionary)weakReference.Target);
                }
                else
                {
                    AddToCache();
                }
            }

        }
    }

    private void AddToCache()
    {
        base.Source = _source;
        if (_cache.ContainsKey(_source))
        {
            _cache.Remove(_source);
        }
        _cache.Add(_source, new WeakReference(this, false));
    }
}
</pre></p>
<p>In my opinion, this solution is the means to an end, and one should re-think and re-work how resources are being used. In the short run to reduce memory footprint, this is a good fix. One potential problem I experience with this is that Expression Blend 2 does not like derived Resource Dictionary. <a href="http://adamkinney.wordpress.com/2010/05/04/design-time-resources-in-expression-blend-4-rc/" target="_blank">Expression Blend 4 can work around this problem with the new Design Time Resource Dictionary feature</a>. If you don&#8217;t have Blend 4 (like us), consider other poor man&#8217;s approach. My approach was to write  two Nant scripts to find and replace Resource Dictionaries to make it Blend friendly, the other to reverse it back.</p>
<p>Happy coding!</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/&amp;title=ResourceDictionary: Use with care" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1046/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1046/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1046&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/08/25/resourcedictionary-use-with-care/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>WPF DynamicResource memory leak</title>
		<link>http://codeblitz.wordpress.com/2010/08/23/wpf-dynamicresource-memory-leak/</link>
		<comments>http://codeblitz.wordpress.com/2010/08/23/wpf-dynamicresource-memory-leak/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 12:12:30 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1044</guid>
		<description><![CDATA[After some troubleshooting on WPF memory leaks, I came across a memory leak that occurs when using DynamicResource on resource keys declared in Application.Resources. You can read about this problem being discussed on msdn forum and stackoverflow. To fix this problem, fortunately there&#8217;s a patch for .NET 3.5 and .NET 4.0 resolved this issue. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1044&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After some troubleshooting on WPF memory leaks, I came across a memory leak that occurs when using DynamicResource on resource keys declared in Application.Resources. You can read about this problem being discussed on <a href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/b97a5f83-5394-430e-9a78-9d3a957e3537/" target="_blank">msdn forum</a> and <a href="http://stackoverflow.com/questions/628386/usage-of-dynamicresource-to-an-application-level-resource-can-cause-leaks" target="_blank">stackoverflow</a>. To fix this problem, fortunately there&#8217;s a <a href="http://support.microsoft.com/?kbid=967328" target="_blank">patch for .NET 3.5</a> and .NET 4.0 resolved this issue.</p>
<p>If you&#8217;re developing in Silverlight, no issue as there&#8217;s no DynamicResource. If you&#8217;re developing in WPF, I urge you to consider if there&#8217;s a need at all to use DynamicResource. If you&#8217;re not using SystemColors, SystemFonts or themes in your application, StaticResource should be considered because DynamicResource are re-evaluated at runtime whenever the resource is requested which is an overhead. If you&#8217;re not familiar about their usages, you can <a href="http://msdn.microsoft.com/en-us/library/ms750613.aspx" target="_blank">read more about them here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1044/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1044/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1044/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1044&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/08/23/wpf-dynamicresource-memory-leak/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>
	</item>
		<item>
		<title>WPF memory leaks</title>
		<link>http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/</link>
		<comments>http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 12:34:19 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[memory leak]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1037</guid>
		<description><![CDATA[After working on a WPF application for a couple of months, I&#8217;ve noticed that the application was leaking a large chunk of memory. After some memory profiling, the culprit was not very apparent. It seems that it was a combination of issues that contributed to the leak. One of the biggest problem was that data-binding [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1037&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After working on a WPF application for a couple of months, I&#8217;ve noticed that the application was leaking a large chunk of memory. After some memory profiling, the culprit was not very apparent. It seems that it was a combination of issues that contributed to the leak. One of the biggest problem was that data-binding was not done &#8220;properly&#8221;.</p>
<p>So what do I mean by &#8220;data-binding not done properly? Apparently if your DataContext does not implement <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" target="_blank">INotifyPropertyChanged</a>, you&#8217;re in big trouble!! Here&#8217;s a<a href="http://support.microsoft.com/kb/938416" target="_blank"> link from Microsoft that explains the phenomenon&#8230;.</a></p>
<p>After much research, I came across a fantastic article explaining all the possible errors one can make to cause a memory leak. Some of these are not WPF inherent problems, they are just typical memory leak culprits dominant in all .NET applications, e.g. failure to unsubscribe an event handler; while some are just obscure and weird. But rest assured that some of the weird ones are fixed in .NET Framework 3.5 SP1 and .NET Framework 4&#8230;. <a href="http://blogs.msdn.com/b/jgoldb/archive/2008/02/04/finding-memory-leaks-in-wpf-based-applications.aspx" target="_blank">So if you develop in WPF, this article is a MUST READ!!!!!</a></p>
<p>Lastly, a word of caution. Remember to start performance testing and memory profiling quite early in the stages of development. It&#8217;s a misconception that these activities can be put off until the late stages. If you do that, you have to bear the consequence of not being able to fix these problems because it becomes too hard to change code and change design&#8230;you have been warned.</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/&amp;title=WPF memory leaks" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1037/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1037/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1037/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1037&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/08/09/wpf-memory-leaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>Dude where&#8217;s my byte?</title>
		<link>http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/</link>
		<comments>http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 11:46:25 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=1027</guid>
		<description><![CDATA[I&#8217;ve encountered a very interesting problem today, the mystery of the missing byte. It all began when one of tests starting failing on my development machine after I got latest from repository. The problem revolves around saving a checksum (byte array) to a varbinary column on SQL Server 2005. The checksum that was generated in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1027&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve encountered a very interesting problem today, the mystery of the missing byte. It all began when one of tests starting failing on my development machine after I got latest from repository. The problem revolves around saving a checksum (byte array) to a varbinary column on SQL Server 2005. The checksum that was generated in code (which is unique for each machine) has a trailing byte of zero in the byte array, and after being saved to SQL Server, the zero byte is missing!!!!!</p>
<p>It was doing my head in, I went through the code and database creation scripts, and finally managed to find the problem. Apparently in the database scripts, there was a line &#8216;<span style="color:#0000ff;"><strong>SET ANSI_PADDING OFF</strong></span>&#8216;. <span style="text-decoration:underline;">This tells SQL Server to truncate off trailing zeros for varbinary and trailing spaces for varchar types</span>. This setting takes into effect during table creation, so whatever you specified stays in effect after. In order to change &#8220;setting&#8221; for that table, you have to drop the table and re-create it with new setting, like so..</p>
<p><pre class="brush: sql;">
DROP TABLE T1;

SET ANSI_PADDING ON;
GO

CREATE TABLE Table1
-- and so on

SET ANSI_PADDING OFF;
GO

</pre></p>
<p>MSDN has recommended that you leave the setting to <strong>ON</strong>, and for very good reason. I doubt you would want things to auto-magically disappear or be modified without your knowledge, but should be should be made explicit and known. It would have saved me hours of troubleshooting, and I hope it will save hours for you too! Please see the <a href="http://technet.microsoft.com/en-us/library/ms187403(SQL.90).aspx" target="_blank">ANSI_PADDING MSDN page</a> for more info.</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/&amp;title=Dude where's my byte?" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/1027/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/1027/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=1027&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/07/28/dude-wheres-my-byte/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>Strategy and Decorator Pattern</title>
		<link>http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/</link>
		<comments>http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 12:53:43 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=996</guid>
		<description><![CDATA[It&#8217;s been a long time since I posted anything, been very busy with work. Many things have changed and I am currently working as an independent contractor.  My latest gig at the moment is working on a WPF product! woo hoo! Today I&#8217;m going to talk about using the Strategy and Decorator Pattern together, as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=996&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since I posted anything, been very busy with work. Many things have changed and I am currently working as an independent contractor.  My latest gig at the moment is working on a WPF product! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  woo hoo!</p>
<p>Today I&#8217;m going to talk about using the <a href="http://www.dofactory.com/Patterns/PatternStrategy.aspx" target="_blank">Strategy</a> and <a href="http://www.dofactory.com/Patterns/PatternDecorator.aspx" target="_blank">Decorator</a> Pattern together, as it turns out they work pretty well together. Strategy pattern is a good way to encapsulate your algorithms, or in my case I want to use it to encapsulate reusable code, following the principle to keep it <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank">DRY</a>. With the introduction of<a href="http://msdn.microsoft.com/en-us/library/018hxwa8.aspx" target="_blank"> Action</a> and <a href="http://msdn.microsoft.com/en-us/library/bb534960.aspx" target="_blank">Func</a> classes in .NET framework, I have found that writing callbacks to be so much more accessible. For instance, you may sometimes need to repeat lots of Try&#8230;Catch statements in your code, and find that hard to keep DRY. This is where using callbacks are useful, like so&#8230;</p>
<p><span id="more-996"></span></p>
<p><pre class="brush: csharp; wrap-lines: false;">
private void ExecuteSomeMethod()
{
    SomeManager someManager = new SomeManager();
    WithExeptionHandling(someManager.Execute);
}

private void WithExeptionHandling(Action callback)
{
    try
    {
        callback();
    }
    catch(Exception e)
    {
        Logger.Log(e.Message);
        throw;
    }
}
</pre></p>
<p>This block of code becomes a good candidate for re-usability, and there are heaps of such blocks of code. For instance, logging code to log start and end perhaps for performance monitoring. Another example is transaction scopes, as well as opening and closing of database connections. Another advantage of using these &#8220;strategies&#8221; is that it coerces the developer to use these conventions and learn good habits.</p>
<p>First of all, I create an interface and a abstract base class to encapsulate the common behavior, as well as to construct the decorator behavior for my strategies.</p>
<p><pre class="brush: csharp; wrap-lines: false;">
public interface IStrategy
{
    StrategyResult Execute();
}

public abstract class BaseStrategy
{
    private readonly Func function;
    private readonly IStrategy strategy;

    protected BaseStrategy(Func func)
    {
        function = func;
    }

    protected BaseStrategy(IStrategy strategy)
    {
        this.strategy = strategy;
    }

    protected StrategyResult InternalExecute()
    {
        if (function != null)
        {
            return new StrategyResult(function());
        }
        return strategy.Execute();
    }
}
</pre></p>
<p>I&#8217;ve create three strategies for this exercise, namely exception handling, transaction scope and logging. Here&#8217;s the exception handling strategy.</p>
<p><pre class="brush: csharp; wrap-lines: false;">
public class ExceptionHandlingStrategy : BaseStrategy, IStrategy
{
    public ExceptionHandlingStrategy(Func func)
        : base(func)
    {
    }

    public ExceptionHandlingStrategy(IStrategystrategy)
        :base(strategy)
    {
    }

    public StrategyResultExecute()
    {
        StrategyResult result;
        try
        {
            result = InternalExecute();
        }
        catch (Exception e)
        {
            result = new StrategyResult(e);
        }
        return result;
    }
}
</pre></p>
<p>Here comes the decorator pattern. If you have notices in the abstract base class earlier on, I have 2 constructors, one which takes an IStrategy&lt;TResult&gt;.  This allows me to wrap many strategies together, essentially getting a chaining effect. For instance, I could easily construct a strategy that logs my method start and end, that handles exception as well as wrap a transaction scope around the call. The possibilities are endless! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To make it easier to construct these decorated chaining effect, I created a factory as well as Extension Methods to make it much sweeter to construct.</p>
<p><pre class="brush: csharp; wrap-lines: false;">
public class StrategyFactory
{
    public IStrategy CreateExceptionHandlingStrategy(Func func)
    {
        return new ExceptionHandlingStrategy(func);
    }

    public IStrategy CreateTransactionScopeStrategy(Func func)
    {
        return new TransactionScopeStrategy(func);
    }

    public IStrategy CreateLoggingStrategy(Func func)
    {
        return new LoggingStrategy(func);
    }
}

public static class StrategyExtensions
{
    public static IStrategy WithExceptionHandling(this IStrategy strategy)
    {
        return new ExceptionHandlingStrategy(strategy);
    }

    public static IStrategy WithTransactionScope(this IStrategy strategy)
    {
        return new TransactionScopeStrategy(strategy);
    }

    public static IStrategyWithLogging(this IStrategy strategy)
    {
        return new LoggingStrategy(strategy);
    }
}
</pre></p>
<p>So now to wrap it all up with sample code that makes use of all these.</p>
<p><pre class="brush: csharp; wrap-lines: false;">
int top = 1;
int bottom = 0;
var factory = new StrategyFactory();

var strategy =
    factory.CreateExceptionHandlingStrategy(() =&gt; top / bottom)
        .WithLogging().WithTransactionScope();

StrategyResult result = strategy.Execute();

Console.WriteLine(&quot;Result Has Error: {0}&quot;, result.HasError);
</pre></p>
<p>As you can see, it&#8217;s easy now to chain your strategies together. From the sample, I will get an exception for dividing one by zero, but handled by the exception handling strategy.</p>
<p>I hope this entry has given you some good ideas toward maintaining your code base. <a href="http://cid-2c6600f1c1d5e3be.office.live.com/self.aspx/.Public/DecoratedStrategy.zip" target="_blank">Download this sample here</a>.</p>
<table border="0" cellspacing="1" cellpadding="1">
<tbody>
<tr>
<td class="sbmText">Share this post :</td>
<td><a title="Post it to del.icio.us" href="http://del.icio.us/post?url=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" border="0" alt="" /></a></td>
<td><a title="Post it to digg" href="http://digg.com/submit?phase=2&amp;url=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" border="0" alt="" /></a></td>
<td><a title="Post it to dotnetkicks" href="http://www.dotnetkicks.com/kick/?url=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" border="0" alt="" /></a></td>
<td><a title="Post it to live" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" border="0" alt="" /></a></td>
<td><a title="Post it to reddit!" href="http://reddit.com/submit?url=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" border="0" alt="" /></a></td>
<td><a title="Post it to technorati!" href="http://technorati.com/faves/?add=http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/&amp;title=Strategy and Decorator Pattern" target="_blank"><img src="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" border="0" alt="" /></a></td>
</tr>
</tbody>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/996/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/996/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/996/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=996&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2010/07/22/strategy-and-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/deliciou4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/digg14.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/CropperCapture154.jpg" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/live4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/reddit4.png" medium="image" />

		<media:content url="http://blogs.msdn.com/blogfiles/rahulso/WindowsLiveWriter/IconsfordifferentSocialBookmarkingSites_B387/technora4.png" medium="image" />
	</item>
		<item>
		<title>Silverlight 4 Beta released</title>
		<link>http://codeblitz.wordpress.com/2009/11/19/silverlight-4-beta-released/</link>
		<comments>http://codeblitz.wordpress.com/2009/11/19/silverlight-4-beta-released/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 00:59:45 +0000</pubDate>
		<dc:creator>Ed Foh</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://codeblitz.wordpress.com/?p=992</guid>
		<description><![CDATA[Interesting news from PDC in LA, Silverlight 4 beta has been released, and comes full a very impressive suite of features and toolset supported by VS 2010. If you have VS 2010 beta installed, you can play around with SL4. Read more about it here.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=992&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Interesting news from PDC in LA, Silverlight 4 beta has been released, and comes full a very impressive suite of features and toolset supported by VS 2010.</p>
<p>If you have VS 2010 beta installed, you can play around with SL4.</p>
<p>Read more about it <a href="http://silverlight.net/getstarted/silverlight-4-beta/" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeblitz.wordpress.com/992/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeblitz.wordpress.com/992/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeblitz.wordpress.com/992/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=codeblitz.wordpress.com&amp;blog=6548671&amp;post=992&amp;subd=codeblitz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://codeblitz.wordpress.com/2009/11/19/silverlight-4-beta-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6486250b0010685d736605a60d466996?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ed Foh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
