<?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>Exceptional Code</title>
	<atom:link href="http://exceptionalcode.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://exceptionalcode.wordpress.com</link>
	<description>catch { }</description>
	<lastBuildDate>Mon, 16 Jan 2012 14:12:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='exceptionalcode.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/8656f86cea7d2505f3e659146a5602d6?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Exceptional Code</title>
		<link>http://exceptionalcode.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://exceptionalcode.wordpress.com/osd.xml" title="Exceptional Code" />
	<atom:link rel='hub' href='http://exceptionalcode.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Code Trivia #7</title>
		<link>http://exceptionalcode.wordpress.com/2011/11/29/code-trivia-7/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/11/29/code-trivia-7/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 21:54:56 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Trivia]]></category>
		<category><![CDATA[code trivia]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=368</guid>
		<description><![CDATA[Lets go for another code trivia, it&#8217;s business as usual, you just need to find what&#8217;s wrong with the following code: TIP: There&#8217;s something very wrong with this specific code and there&#8217;s also another subtle problem that arises due to how the code is structured. As explained in the comments, the using statement expands to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=368&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Lets go for  another code trivia, it&#8217;s business as usual, you just need to find what&#8217;s wrong with the following code:</p>
<p><pre class="brush: csharp;">
static void Main(string[] args)
{
    using (var file = new FileStream(&quot;test&quot;, FileMode.Create) { WriteTimeout = 1 })
    {
        file.WriteByte(0);
    }
}
</pre></p>
<p><strong>TIP:</strong> There&#8217;s something very wrong with this specific code and there&#8217;s also another subtle problem that arises due to how the code is structured.</p>
<hr />
As explained in the comments, the using statement expands to the following code leading to the FileStream object never getting disposed because the finally is never executed.</p>
<p><pre class="brush: csharp;">
FileStream file = new FileStream(&quot;test&quot;, FileMode.Create) { WriteTimeout = 1 };
try
{
    file.WriteByte(0);
}
finally
{
    if (file != null)
        ((IDisposable)file).Dispose();
}
</pre></p>
<p>With this in mind, one could be tempted to use a custom try/finally block for handling this situation. Something like this:</p>
<p><pre class="brush: csharp;">
FileStream file = null;
try
{
    file = new FileStream(&quot;test&quot;, FileMode.Create)
    {
        WriteTimeout = 1
    };

    file.WriteByte(0);
}
finally
{
    if (file != null)
        file.Dispose();
}
</pre></p>
<p>However this suffers from the same problem but due to a different cause. Even though the constructor for the object is called inside the try clause the object initializer syntax prevents it from being assigned to the <code>file</code> variable before all properties have been initialized, leading to the file stream still not being disposed.</p>
<p>The expansion caused by the object initializer syntax is shown in the next sample of code:</p>
<p><pre class="brush: csharp;">
FileStream file = null;
try
{
    FileStream compilerGenerated = new FileStream(&quot;test&quot;, FileMode.Create);

    compilerGenerated.WriteTimeout = 1;

    file = compilerGenerated;

    file.WriteByte(0);
}
finally
{
    if (file != null)
        file.Dispose();
}
</pre></p>
<p>Basically, you need to be careful with using blocks where the resource initialization expression may throw an exception and you also need to pay careful attention on the subtleties of using object initializer syntax. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/368/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/368/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/368/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=368&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/11/29/code-trivia-7/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Razor &#8211; Hiding a Section in a Layout</title>
		<link>http://exceptionalcode.wordpress.com/2011/11/23/razor-hiding-a-section-in-a-layout/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/11/23/razor-hiding-a-section-in-a-layout/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 22:26:25 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP .NET]]></category>
		<category><![CDATA[ASP .NET MVC]]></category>
		<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[ASP .NET MVC 3]]></category>
		<category><![CDATA[Razor]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=363</guid>
		<description><![CDATA[Layouts in Razor allow you to define placeholders named sections where content pages may insert custom content much like the ContentPlaceHolder available in ASPX master pages. When you define a section in a Razor layout it&#8217;s possible to specify if the section must be defined in every content page using the layout or if its [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=363&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Layouts in Razor allow you to define placeholders named sections where content pages may insert custom content much like the <code>ContentPlaceHolder</code> available in ASPX master pages.</p>
<p>When you define a section in a Razor layout it&#8217;s possible to specify if the section must be defined in every content page using the layout or if its definition is optional allowing a page not to provide any content for that section. For the latter case, it&#8217;s also possible using the <code>IsSectionDefined</code> method to render default content when a page does not define the section.</p>
<p>However if you ever require to hide a given section from all pages based on some runtime condition you might be tempted to conditionally define it in the layout much like in the following code snippet.</p>
<p><pre class="brush: csharp;">
if(condition) {
	@RenderSection(&quot;ConditionalSection&quot;, false)
}
</pre></p>
<p>With this code you&#8217;ll hit an error as soon as any content page provides content for the section which makes sense since if a page inherits a layout then it should only define sections that are also defined in it.</p>
<p>To workaround this scenario you have a couple of options. Make the given section optional with and move the condition that enables or disables it to every content page. This leads to code duplication and future pages may forget to only define the section based on that same condition.</p>
<p>The other option is to conditionally define the section in the layout page using the following hack:</p>
<p><pre class="brush: csharp;">
@{
    if(condition) {
        @RenderSection(&quot;ConditionalSection&quot;, false)
    } 
    else {
        RenderSection(&quot;ConditionalSection&quot;, false).WriteTo(TextWriter.Null);
    }
}
</pre></p>
<p>Hack inspired by a <a href="http://stackoverflow.com/questions/8040453/how-do-i-optionally-render-a-section-in-asp-net-mvc-3" target="_blank">recent stackoverflow question</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/363/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=363&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/11/23/razor-hiding-a-section-in-a-layout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit Testing with NUnit and Moles Redux</title>
		<link>http://exceptionalcode.wordpress.com/2011/10/15/unit-testing-with-nunit-and-moles-redux/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/10/15/unit-testing-with-nunit-and-moles-redux/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 11:20:58 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Moles]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=350</guid>
		<description><![CDATA[Almost two years ago, when Moles was still being packaged alongside Pex, I wrote a post on how to run NUnit tests supporting moled types. A lot has changed since then and Moles is now being distributed independently of Pex, but maintaining support for integration with NUnit and other testing frameworks. For NUnit the support [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=350&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Almost two years ago, when <a href="http://research.microsoft.com/en-us/projects/moles/">Moles</a> was still being packaged alongside <a href="http://research.microsoft.com/en-us/projects/pex/">Pex</a>, I wrote a post on <a href="http://exceptionalcode.wordpress.com/2009/12/08/unit-testing-with-nunit-and-moles/">how to run NUnit tests supporting moled types</a>.</p>
<p>A lot has changed since then and Moles is now being distributed independently of Pex, but maintaining support for integration with NUnit and other testing frameworks.</p>
<p>For NUnit the support is provided by an addin class library (<em>Microsoft.Moles.NUnit.dll</em>) that you need to reference in your test project so that you can decorate yours tests with the <code>MoledAttribute</code>. The addin DLL must also be placed in the addins folder inside the NUnit installation directory.</p>
<p>There is however a downside, since Moles and NUnit follow a different release cycle and the addin DLL must be built against a specific NUnit version, you may find that the release included with the latest version of Moles does not work with your version of NUnit.</p>
<p>Fortunately the code for building the NUnit addin is supplied in the archive (<em>moles.samples.zip</em>) that you can found in the <em>Documentation</em> folder inside the Moles installation directory. By rebuilding the addin against your specific version of NUnit you are able to support any version.</p>
<p>Also to note that in Moles 0.94.51023.0 the addin code did not support the use of <code>TestCaseAttribute</code> in your moled tests. However, if you need this support, you need to make just a couple of changes. </p>
<p>Change the <code>ITestDecorator.Decorate</code> method in the <code>MolesAddin</code> class:</p>
<p><pre class="brush: csharp;">
Test ITestDecorator.Decorate(Test test, MemberInfo member)
{
    SafeDebug.AssumeNotNull(test, &quot;test&quot;);
    SafeDebug.AssumeNotNull(member, &quot;member&quot;);

    bool isTestFixture = true;
    isTestFixture &amp;= test.IsSuite;
    isTestFixture &amp;= test.FixtureType != null;

    bool hasMoledAttribute = true;
    hasMoledAttribute &amp;= !SafeArray.IsNullOrEmpty(
        member.GetCustomAttributes(typeof(MoledAttribute), false));

    if (!isTestFixture &amp;&amp; hasMoledAttribute)
    {
        return new MoledTest(test);
    }

    return test;
}
</pre></p>
<p>Change the <code>Tests</code> property in the <code>MoledTest</code> class:</p>
<p><pre class="brush: csharp;">
public override System.Collections.IList Tests
{
    get
    {
        if (this.test.Tests == null)
        {
            return null;
        }

        var moled = new List&lt;Test&gt;(this.test.Tests.Count);

        foreach (var test in this.test.Tests)
        {
            moled.Add(new MoledTest((Test)test));
        }

        return moled;
    }
}
</pre></p>
<p><em>Disclaimer:</em> I only tested this implementation against NUnit 2.5.10.11092 version.</p>
<p>Finally you just need to run the NUnit console runner through the Moles runner. A quick example follows:</p>
<p><pre class="brush: bash;">
moles.runner.exe [Tests.dll] /r:nunit-console.exe /x86 /args:[NUnitArgument1] /args:[NUnitArgument2]
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/350/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/350/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/350/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=350&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/10/15/unit-testing-with-nunit-and-moles-redux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Assembly Resources Expression Builder</title>
		<link>http://exceptionalcode.wordpress.com/2011/07/23/assembly-resources-expression-builder/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/07/23/assembly-resources-expression-builder/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 10:15:50 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP .NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=345</guid>
		<description><![CDATA[In ASP.NET you can tackle the internationalization requirement by taking advantage of native support to local and global resources used with the ResourceExpressionBuilder. But with this approach you cannot access public resources defined in external assemblies referenced by your Web application. However, since you can extend the .NET resource provider mechanism and create new expression [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=345&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In ASP.NET you can tackle the internationalization requirement by taking advantage of native support to <a href="http://msdn.microsoft.com/en-us/library/ms227427.aspx">local and global resources</a> used with the <a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.resourceexpressionbuilder.aspx">ResourceExpressionBuilder</a>.</p>
<p>But with this approach you cannot access public resources defined in external assemblies referenced by your Web application.</p>
<p>However, since you can extend the .NET resource provider mechanism and create new expression builders you can workaround this limitation and use external resources in your ASPX pages much like you use local or global resources.</p>
<p>Finally, if you are thinking, okay this is all very nice but where&#8217;s the code I can use? Well, it was too much to publish directly here so download it from <a href="http://webhelpers.codeplex.com/">Helpers.Web@Codeplex</a>, where you also find a sample on how to configure and use it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=345&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/07/23/assembly-resources-expression-builder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>VSTO Troubleshooting Quick Tips</title>
		<link>http://exceptionalcode.wordpress.com/2011/07/03/vsto-troubleshooting-quick-tips/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/07/03/vsto-troubleshooting-quick-tips/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 11:09:20 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[VSTO]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=335</guid>
		<description><![CDATA[If you ever find yourself troubleshooting a VSTO addin that does not load then these steps will interest you. Do not skip the basics and check the registry at HKLM\Software\Microsoft\Office\&#60;Application&#62;\AddIns\&#60;AddInName&#62; or HKCU\Software\Microsoft\Office\&#60;Product&#62;\AddIns\&#60;Application&#62; because if the LoadBehavior key is not set to 3 the office application will not even try to load it on startup; Enable [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=335&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you ever find yourself troubleshooting a VSTO addin that does not load then these steps will interest you.</p>
<ol>
<li>Do not skip the basics and check the registry at <code>HKLM\Software\Microsoft\Office\&lt;Application&gt;\AddIns\&lt;AddInName&gt;</code> or <code>HKCU\Software\Microsoft\Office\&lt;Product&gt;\AddIns\&lt;Application&gt;</code> because if the <a href="http://msdn.microsoft.com/en-us/library/bb386106.aspx#LoadBehavior">LoadBehavior</a> key is not set to 3 the office application will not even try to load it on startup;</li>
<li>Enable error alerts popups by configuring an environment variable<br />
<pre class="brush: bash;">SET VSTO_SUPPRESSDISPLAYALERTS=0</pre>
</li>
<li>Enable logging errors to file by configuring an environment variable<br />
<pre class="brush: plain;">SET VSTO_LOGALERTS=1</pre>
</li>
<li>Pray for an error alert popup or for an error in the log file so that you can fix its cause.</li>
</ol>
<p>&nbsp;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/335/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=335&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/07/03/vsto-troubleshooting-quick-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Visual Studio Macro &#8211; Identifier to String Literal</title>
		<link>http://exceptionalcode.wordpress.com/2011/06/11/visual-studio-macro-identifier-to-string-literal/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/06/11/visual-studio-macro-identifier-to-string-literal/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 10:55:49 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=330</guid>
		<description><![CDATA[When implementing public methods with parameters it&#8217;s important to write boiler-plate code to do argument validation and throw exceptions when needed, ArgumentException and ArgumentNullException being the most recurrent. Another thing that is important is to correctly specify the parameter causing the exception through the proper exception constructor. In order to take advantage of IntelliSense completion [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=330&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When implementing public methods with parameters it&#8217;s important to write boiler-plate code to do argument validation and throw exceptions when needed, <em>ArgumentException</em> and <em>ArgumentNullException</em> being the most recurrent.</p>
<p>Another thing that is important is to correctly specify the parameter causing the exception through the proper exception constructor.</p>
<p>In order to take advantage of IntelliSense completion in these scenarios I use a Visual Studio macro binded to a keyboard shortcut that converts the identifier at the cursor position to a string literal.</p>
<p>And here&#8217;s the macro:</p>
<p><pre class="brush: vb;">
Sub ConvertIdentifierToStringLiteral()
    Dim targetWord As String
    Dim document As EnvDTE.TextDocument

    document = CType(DTE.ActiveDocument.Object, EnvDTE.TextDocument)

    If document.Selection.Text.Length &gt; 0 Then
        targetWord = document.Selection.Text
        document.Selection.ReplacePattern(targetWord, &quot;&quot;&quot;&quot; + targetWord + &quot;&quot;&quot;&quot;)
    Else
        Dim cursorPoint As EnvDTE.TextPoint

        cursorPoint = document.Selection.ActivePoint()

        Dim editPointLeft As EnvDTE.EditPoint
        Dim editPointRight As EnvDTE.EditPoint

        editPointLeft = cursorPoint.CreateEditPoint()
        editPointLeft.WordLeft(1)

        editPointRight = editPointLeft.CreateEditPoint()
        editPointRight.WordRight(1)

        targetWord = editPointLeft.GetText(editPointRight)
        editPointLeft.ReplaceText(editPointRight, &quot;&quot;&quot;&quot; + targetWord + &quot;&quot;&quot;&quot;, 0)
    End If
End Sub
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/330/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/330/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/330/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=330&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/06/11/visual-studio-macro-identifier-to-string-literal/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Extended FindWindow</title>
		<link>http://exceptionalcode.wordpress.com/2011/05/22/extended-findwindow/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/05/22/extended-findwindow/#comments</comments>
		<pubDate>Sun, 22 May 2011 19:21:41 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=324</guid>
		<description><![CDATA[The Win32 API provides the FindWindow function that supports finding top-level windows by their class name and/or title. However, the title search does not work if you are trying to match partial text at the middle or the end of the full window title. You can however implement support for these extended search features by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=324&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Win32 API provides the <a href="http://msdn.microsoft.com/en-us/library/ms633499%28VS.85%29.aspx" target="_blank">FindWindow</a> function that supports finding top-level windows by their class name and/or title. However, the title search does not work if you are trying to match partial text at the middle or the end of the full window title.</p>
<p>You can however implement support for these extended search features by using another set of Win32 API like <a href="http://msdn.microsoft.com/en-us/library/ms633497%28VS.85%29.aspx" target="_blank">EnumWindows</a> and <a href="http://msdn.microsoft.com/en-us/library/ms633520%28VS.85%29.aspx" target="_blank">GetWindowText</a>. A possible implementation follows:</p>
<p><pre class="brush: csharp; collapse: true; light: false; toolbar: true;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

public class WindowInfo
{
    private IntPtr handle;

    private string className;

    internal WindowInfo(IntPtr handle, string title)
    {
        if (handle == IntPtr.Zero)
            throw new ArgumentException(&quot;Invalid handle.&quot;, &quot;handle&quot;);

        this.Handle = handle;
        this.Title = title ?? string.Empty;
    }

    public string Title { get; private set; }

    public string ClassName
    {
        get
        {
            if (className == null)
            {
                className = GetWindowClassNameByHandle(this.Handle);
            }

            return className;
        }
    }

    public IntPtr Handle
    {
        get
        {
            if (!NativeMethods.IsWindow(this.handle))
                throw new InvalidOperationException(&quot;The handle is no longer valid.&quot;);

            return this.handle;
        }
        private set { this.handle = value; }
    }

    public static WindowInfo[] EnumerateWindows()
    {
        var windows = new List&lt;WindowInfo&gt;();

        NativeMethods.EnumWindowsProcessor processor = (hwnd, lParam) =&gt;
        {
            windows.Add(new WindowInfo(hwnd, GetWindowTextByHandle(hwnd)));

            return true;
        };

        bool succeeded = NativeMethods.EnumWindows(processor, IntPtr.Zero);

        if (!succeeded)
            return new WindowInfo[] { };

        return windows.ToArray();
    }

    public static WindowInfo FindWindow(Predicate&lt;WindowInfo&gt; predicate)
    {
        WindowInfo target = null;

        NativeMethods.EnumWindowsProcessor processor = (hwnd, lParam) =&gt;
        {
            var current = new WindowInfo(hwnd, GetWindowTextByHandle(hwnd));

            if (predicate(current))
            {
                target = current;

                return false;
            }

            return true;
        };

        NativeMethods.EnumWindows(processor, IntPtr.Zero);

        return target;
    }

    private static string GetWindowTextByHandle(IntPtr handle)
    {
        if (handle == IntPtr.Zero)
            throw new ArgumentException(&quot;Invalid handle.&quot;, &quot;handle&quot;);

        int length = NativeMethods.GetWindowTextLength(handle);

        if (length == 0)
            return string.Empty;

        var buffer = new StringBuilder(length + 1);

        NativeMethods.GetWindowText(handle, buffer, buffer.Capacity);

        return buffer.ToString();
    }

    private static string GetWindowClassNameByHandle(IntPtr handle)
    {
        if (handle == IntPtr.Zero)
            throw new ArgumentException(&quot;Invalid handle.&quot;, &quot;handle&quot;);

        const int WindowClassNameMaxLength = 256;

        var buffer = new StringBuilder(WindowClassNameMaxLength);

        NativeMethods.GetClassName(handle, buffer, buffer.Capacity);

        return buffer.ToString();
    }
}

internal class NativeMethods
{
    public delegate bool EnumWindowsProcessor(IntPtr hwnd, IntPtr lParam);

    [DllImport(&quot;user32.dll&quot;)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumWindows(
        EnumWindowsProcessor lpEnumFunc,
        IntPtr lParam);

    [DllImport(&quot;user32.dll&quot;, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetWindowText(
        IntPtr hWnd,
        StringBuilder lpString,
        int nMaxCount);

    [DllImport(&quot;user32.dll&quot;, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport(&quot;user32.dll&quot;, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetClassName(
        IntPtr hWnd,
        StringBuilder lpClassName,
        int nMaxCount);

    [DllImport(&quot;user32.dll&quot;)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindow(IntPtr hWnd);
}
</pre></p>
<p>The access to the windows handle is preceded by a sanity check to assert if it&#8217;s still valid, but if you are dealing with windows out of your control then the window can be destroyed right after the check so it&#8217;s not guaranteed that you&#8217;ll get a valid handle.</p>
<p>Finally, to wrap this up a usage, example:</p>
<p><pre class="brush: csharp;">
static void Main(string[] args)
{
    var w = WindowInfo.FindWindow(wi =&gt; wi.Title.Contains(&quot;Test.docx&quot;));

    if (w != null)
    {
        Console.Write(w.Title);
    }
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/324/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/324/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/324/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=324&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/05/22/extended-findwindow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Inside BackgroundWorker</title>
		<link>http://exceptionalcode.wordpress.com/2011/05/03/inside-backgroundworker/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/05/03/inside-backgroundworker/#comments</comments>
		<pubDate>Tue, 03 May 2011 20:58:51 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=320</guid>
		<description><![CDATA[The BackgroundWorker is a reusable component that can be used in different contexts, but sometimes with unexpected results. If you are like me, you have mostly used background workers while doing Windows Forms development due to the flexibility they offer for running a background task. They support cancellation and give events that signal progress updates [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=320&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The BackgroundWorker is a reusable component that can be used in different contexts, but sometimes with unexpected results.</p>
<p>If you are like me, you have mostly used background workers while doing Windows Forms development due to the flexibility they offer for running a background task. They support cancellation and give events that signal progress updates and task completion.</p>
<p>When used in Windows Forms, these events (ProgressChanged and RunWorkerCompleted) get executed back on the UI thread where you can freely access your form controls.</p>
<p>However, the logic of the progress changed and worker completed events being invoked in the thread that started the background worker is not something you get directly from the BackgroundWorker, but instead from the fact that you are running in the context of Windows Forms.</p>
<p>Take the following example that illustrates the use of a worker in three different scenarios:</p>
<p> &#8211; Console Application or Windows Service;<br />
 &#8211; Windows Forms;<br />
 &#8211; WPF.</p>
<p><pre class="brush: csharp;">
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;

class Program
{
    static AutoResetEvent Synch = new AutoResetEvent(false);

    static void Main()
    {
        var bw1 = new BackgroundWorker();
        var bw2 = new BackgroundWorker();
        var bw3 = new BackgroundWorker();

        Console.WriteLine(&quot;DEFAULT&quot;);
        var unspecializedThread = new Thread(() =&gt;
        {
            OutputCaller(1);

            SynchronizationContext.SetSynchronizationContext(
                new SynchronizationContext());

            bw1.DoWork += (sender, e) =&gt; OutputWork(1);
            bw1.RunWorkerCompleted += (sender, e) =&gt; OutputCompleted(1);
            // Uses default SynchronizationContext
            bw1.RunWorkerAsync();
        });
        unspecializedThread.IsBackground = true;
        unspecializedThread.Start();

        Synch.WaitOne();

        Console.WriteLine();
        Console.WriteLine(&quot;WINDOWS FORMS&quot;);
        var windowsFormsThread = new Thread(() =&gt;
        {
            OutputCaller(2);

            SynchronizationContext.SetSynchronizationContext(
                new WindowsFormsSynchronizationContext());

            bw2.DoWork += (sender, e) =&gt; OutputWork(2);
            bw2.RunWorkerCompleted += (sender, e) =&gt; OutputCompleted(2);
            // Uses WindowsFormsSynchronizationContext
            bw2.RunWorkerAsync();

            Application.Run();
        });
        windowsFormsThread.IsBackground = true;
        windowsFormsThread.SetApartmentState(ApartmentState.STA);
        windowsFormsThread.Start();

        Synch.WaitOne();

        Console.WriteLine();
        Console.WriteLine(&quot;WPF&quot;);
        var wpfThread = new Thread(() =&gt;
        {
            OutputCaller(3);

            SynchronizationContext.SetSynchronizationContext(
                new DispatcherSynchronizationContext());

            bw3.DoWork += (sender, e) =&gt; OutputWork(3);
            bw3.RunWorkerCompleted += (sender, e) =&gt; OutputCompleted(3);
            // Uses DispatcherSynchronizationContext
            bw3.RunWorkerAsync();

            Dispatcher.Run();
        });
        wpfThread.IsBackground = true;
        wpfThread.SetApartmentState(ApartmentState.STA);
        wpfThread.Start();

        Synch.WaitOne();
    }

    static void OutputCaller(int workerId)
    {
        Console.WriteLine(
            &quot;bw{0}.{1} | Thread: {2} | IsThreadPool: {3}&quot;,
            workerId,
            &quot;RunWorkerAsync&quot;.PadRight(18),
            Thread.CurrentThread.ManagedThreadId,
            Thread.CurrentThread.IsThreadPoolThread);
    }

    static void OutputWork(int workerId)
    {
        Console.WriteLine(
            &quot;bw{0}.{1} | Thread: {2} | IsThreadPool: {3}&quot;,
            workerId,
            &quot;DoWork&quot;.PadRight(18),
            Thread.CurrentThread.ManagedThreadId,
            Thread.CurrentThread.IsThreadPoolThread);
    }

    static void OutputCompleted(int workerId)
    {
        Console.WriteLine(
            &quot;bw{0}.{1} | Thread: {2} | IsThreadPool: {3}&quot;,
            workerId,
            &quot;RunWorkerCompleted&quot;.PadRight(18),
            Thread.CurrentThread.ManagedThreadId,
            Thread.CurrentThread.IsThreadPoolThread);

        Synch.Set();
    }
}
</pre></p>
<p>Output:</p>
<p><pre class="brush: csharp;">
//DEFAULT
//bw1.RunWorkerAsync     | Thread: 3 | IsThreadPool: False
//bw1.DoWork             | Thread: 4 | IsThreadPool: True
//bw1.RunWorkerCompleted | Thread: 5 | IsThreadPool: True

//WINDOWS FORMS
//bw2.RunWorkerAsync     | Thread: 6 | IsThreadPool: False
//bw2.DoWork             | Thread: 5 | IsThreadPool: True
//bw2.RunWorkerCompleted | Thread: 6 | IsThreadPool: False

//WPF
//bw3.RunWorkerAsync     | Thread: 7 | IsThreadPool: False
//bw3.DoWork             | Thread: 5 | IsThreadPool: True
//bw3.RunWorkerCompleted | Thread: 7 | IsThreadPool: False
</pre></p>
<p>As you can see the output between the first and remaining scenarios is somewhat different. While in Windows Forms and WPF the worker completed event runs on the thread that called RunWorkerAsync, in the first scenario the same event runs on any thread available in the thread pool.</p>
<p>Another scenario where you can get the first behavior, even when on Windows Forms or WPF, is if you chain the creation of background workers, that is, you create a second worker in the DoWork event handler of an already running worker. Since the DoWork executes in a thread from the pool the second worker will use the default synchronization context and the completed event will not run in the UI thread.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/320/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/320/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/320/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=320&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/05/03/inside-backgroundworker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>1512 Hours Without Internet</title>
		<link>http://exceptionalcode.wordpress.com/2011/05/03/1512-hours-without-internet/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/05/03/1512-hours-without-internet/#comments</comments>
		<pubDate>Tue, 03 May 2011 20:51:44 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=318</guid>
		<description><![CDATA[Tonight&#8217;s the night, that I&#8217;m finally back online after two months of being disconnected from the online world, at least while not at work, but that doesn&#8217;t count. This was completely unplanned and caused by infra-structures problems from my new ISP, but they finally sorted it out and I&#8217;m online again.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=318&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Tonight&#8217;s the night, that I&#8217;m finally back online after two months of being disconnected from the online world, at least while not at work, but that doesn&#8217;t count.</p>
<p>This was completely unplanned and caused by infra-structures problems from my new ISP, but they finally sorted it out and I&#8217;m online again.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/318/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/318/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/318/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=318&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/05/03/1512-hours-without-internet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
		<item>
		<title>VS 2010 SP1 BETA &#8211; App.config XML Transformation Fix</title>
		<link>http://exceptionalcode.wordpress.com/2011/02/25/vs-2010-sp1-beta-app-config-xml-transformation-fix/</link>
		<comments>http://exceptionalcode.wordpress.com/2011/02/25/vs-2010-sp1-beta-app-config-xml-transformation-fix/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 22:46:28 +0000</pubDate>
		<dc:creator>João Angelo</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[App.config]]></category>
		<category><![CDATA[transformations]]></category>
		<category><![CDATA[XDT]]></category>

		<guid isPermaLink="false">http://exceptionalcode.wordpress.com/?p=310</guid>
		<description><![CDATA[The current version for App.config XML tranformations as described in a previous post does not support the SP1 BETA version of Visual Studio. I did some quick tests and decided to provide a different version with a compatibility fix for those already experimenting with the beta release of the service pack. This is a quick [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=310&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The current version for App.config XML tranformations as described in a <a href="http://exceptionalcode.wordpress.com/2010/06/21/visual-studio-app-config-xml-transformation/">previous post</a> does not support the SP1 BETA version of Visual Studio. I did some quick tests and decided to provide a different version with a compatibility fix for those already experimenting with the beta release of the service pack.</p>
<p>This is a quick workaround to the build errors I found when using the transformations in SP1 beta and is pretty much untested since I&#8217;ll wait for the final release of the service pack to take a closer look to any possible problems.</p>
<p>But for now, those that already installed SP1 beta can use the following transformations:</p>
<p><a href="https://gist.github.com/844649">VS 2010 SP1 BETA &#8211; App.config XML Transformation</a></p>
<p>And those with the RTM release of Visual Studio can continue to use the original version of the transformations available from:</p>
<p><a href="https://gist.github.com/447538">VS 2010 RTM &#8211; App.config XML Transformation</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/exceptionalcode.wordpress.com/310/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/exceptionalcode.wordpress.com/310/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/exceptionalcode.wordpress.com/310/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=exceptionalcode.wordpress.com&amp;blog=9452681&amp;post=310&amp;subd=exceptionalcode&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://exceptionalcode.wordpress.com/2011/02/25/vs-2010-sp1-beta-app-config-xml-transformation-fix/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d3ffe69fe8b52f65c6377e3326c017f0?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jmangelo</media:title>
		</media:content>
	</item>
	</channel>
</rss>
