<?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>Joao @ iLab8</title>
	<atom:link href="http://blog.ilab8.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ilab8.com</link>
	<description>360° on .NET Development</description>
	<lastBuildDate>Tue, 26 Mar 2013 07:23:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.ilab8.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Joao @ iLab8</title>
		<link>http://blog.ilab8.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.ilab8.com/osd.xml" title="Joao @ iLab8" />
	<atom:link rel='hub' href='http://blog.ilab8.com/?pushpress=hub'/>
		<item>
		<title>20-12-2012 has past &#8230; 2013 is here &#8230; what&#8217;s next ?</title>
		<link>http://blog.ilab8.com/2013/01/07/20-12-2012-has-past-2013-is-here-whats-next/</link>
		<comments>http://blog.ilab8.com/2013/01/07/20-12-2012-has-past-2013-is-here-whats-next/#comments</comments>
		<pubDate>Mon, 07 Jan 2013 00:18:54 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[Computers and Internet]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=711</guid>
		<description><![CDATA[A new year has started, so happy new year to all of you !!! This blog entry, unlike my previous ones, is not about .NET, although there are a few technical links . The end-of-year period has always been a period for resting, meeting with family and friends, reflecting on the year just past &#8230; thanking [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=711&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2013/01/happynewyear2013_small.png"><img class="alignleft size-full wp-image-715" style="margin:10px;" alt="HappyNewYear2013" src="http://ilab8.files.wordpress.com/2013/01/happynewyear2013_small.png?w=600"   /></a>A new year has started, so happy new year to all of you !!!</p>
<p>This blog entry, unlike my previous ones, is not about .NET, although there are a few technical links <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>The end-of-year period has always been a period for resting, meeting with family and friends, reflecting on the year just past &#8230; thanking god for the good life and the good people around me &#8230; and thinking about what could come soon &#8230;</p>
<p>For resting, I really enjoyed the reading of a comic book serie called &#8220;<a href="http://www.ythaq.com/" target="_blank">Les Naufragés d&#8217;Ythaq</a>&#8221; or in english &#8220;<a title="UK" href="http://www.amazon.co.uk/Soleil-Ythaq-Forsaken-World-Premiere/dp/0785136266/ref=pd_sim_b_1" target="_blank">Ythaq: The Forsaken World</a>&#8220; (see references below):</p>
<p style="text-align:center;"><a href="http://ilab8.files.wordpress.com/2013/01/lesnaufragesdythaq_small.png"><img class="size-full wp-image-717 aligncenter" alt="Les Naufrages d'Ythaq" src="http://ilab8.files.wordpress.com/2013/01/lesnaufragesdythaq_small.png?w=600"   /></a></p>
<p>The scenario is from Christophe Arleston and the drawings by Adrien Floch. I started the reading 3 years ago but the serie was not finished at the time. Now the first cycle (book #1 to #9) is finished &#8211; no more wait, so hurry !<span id="more-711"></span></p>
<p>Obvously, in France, resting is one side of the story &#8211; Family/friends gathering around a good table, i.e. good food and wine, is the other side story &#8211; no more comments.</p>
<p>Going back to Technical subjct, I also read and searched about &#8220;Testing Quadrants&#8221; (big picture <a title="Testing Quadrants" href="http://lh3.ggpht.com/_X3kaawac_g4/S3VCgzOuyQI/AAAAAAAAAvw/aww4Ui2N7LU/agile-testing-quadrants.JPG?imgmax=800" target="_blank">here</a>).</p>
<p><a href="http://ilab8.files.wordpress.com/2013/01/agiletestingquadrants_small.png"><img class="aligncenter size-full wp-image-713" alt="Testing Quadrants" src="http://ilab8.files.wordpress.com/2013/01/agiletestingquadrants_small.png?w=600"   /></a></p>
<p>I have ordered the book &#8220;<a title="Amazon UK" href="http://www.amazon.co.uk/Agile-Testing-Practical-Addison-Wesley-Signature/dp/0321534468" target="_blank">Agile Testing</a>&#8221; written by Lisa Crispin and Janet Gregory, but I have not read it yet. &#8220;Testing Quadrants&#8221; are useful to help any technical team to build a safety net of tests for their system and explore new ways about testing their systems, see further References below.</p>
<p>2013 is here &#8230; I remember back in early 2003, people were saying that 10 years later the programming world would be dominated by Java and .NET languages (C# / VB.NET). Looking at the figures and the graph from <a title="TIOBE Programming Community Index for January 2013" href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" target="_blank">TIOBE.com web site</a>, the TIOBE programming indexes, no one could have forecasted the succes of &#8220;Objective-C&#8221; the language used to develop iOS applications. So forecasts are just forecasts, i.e. an indication, sometimes in the good direction sometimes not. The other interesting point is to note that C is still widely used - C was created beginning of 1970&#8242;s, more than 40 years ago, thanks to <a title="Wikipedia -  Dennis Ritchie" href="http://en.wikipedia.org/wiki/Dennis_Ritchie">Dennis Ritchie</a>. Incredible for a programming language to last that long!!</p>
<p>Another thing I remember at the time in 2003 were the talks about car self-driving. What happened in may 2012?</p>
<p><a title="BBC News - Google gets Nevada driving licence for self-drive car" href="http://www.bbc.co.uk/news/technology-17989553" target="_blank">Google got a Nevada driving licence for self-drive car</a>, a modified Pryus car from Toyata. Whahouh !!</p>
<div id="attachment_712" class="wp-caption aligncenter" style="width: 314px"><a href="http://ilab8.files.wordpress.com/2013/01/googletoyataselfdrivingcar_small.png"><img class="size-full wp-image-712" alt="Google/Toyata Self-driving car" src="http://ilab8.files.wordpress.com/2013/01/googletoyataselfdrivingcar_small.png?w=600"   /></a><p class="wp-caption-text">Google/Toyata Self-driving car</p></div>
<p>And Toyata carries on &#8220;<a title="BBC - Toyota sneak previews self-drive car ahead of tech show" href="http://www.bbc.co.uk/news/technology-20910769" target="_blank">Toyota sneak previews self-drive car ahead of tech show (BBC)</a>&#8220;. Maybe in 10 years time from now, my kids would not have to learn driving. What about old people or disabled people? they will have a car and go where they want.</p>
<p>Now a few more links to keep thinking what might come in the near future :</p>
<ul>
<li>IBM: The 5-in-5 senses computer [<a title="IBM - 5-in-5" href="http://www.ibm.com/smarterplanet/us/en/ibm_predictions_for_future/ideas/index.html" target="_blank">IBM web site</a>] and [BBC web site].</li>
</ul>
<p><a href="http://ilab8.files.wordpress.com/2013/01/ibm5in5_small.png"><img class="size-full wp-image-721 aligncenter" alt="IBM5in5_small" src="http://ilab8.files.wordpress.com/2013/01/ibm5in5_small.png?w=600"   /></a></p>
<ul>
<li>Microsoft : <a title="meet-microsoft-the-worlds-best-kept-randd-secret" href="http://www.pcworld.com/article/2020268/meet-microsoft-the-worlds-best-kept-randd-secret.html" target="_blank">Meet Microsoft, the world&#8217;s best kept R&amp;D secret (PC World)</a>.</li>
</ul>
<p><a href="http://ilab8.files.wordpress.com/2013/01/microsoftphoneontheskin_small.png"><img class="aligncenter size-full wp-image-722" alt="MicrosoftPhoneOnTheSkin_small" src="http://ilab8.files.wordpress.com/2013/01/microsoftphoneontheskin_small.png?w=600"   /></a></p>
<ul>
<li><a title="Bendable phones" href="http://www.bbc.co.uk/news/technology-20526577" target="_blank">Bendable phone and others [BBC]</a>.</li>
</ul>
<p><a href="http://ilab8.files.wordpress.com/2013/01/bendablephones_small.png"><img class="aligncenter size-full wp-image-725" alt="BendablePhones_small" src="http://ilab8.files.wordpress.com/2013/01/bendablephones_small.png?w=600"   /></a></p>
<p>I hoped you enjoyed this first blog entry and if you have other links about what the future might bring us in the next 5 to 10 years, please post them and we will see in 2023 what really happened. Again Happy New Year 2013 to you !!</p>
<h2>References:</h2>
<ul>
<li>Happy New Year 2013 ! picture
<ul>
<li>I took it from a fantastic selection on this <a title="XeBii Mj - 2013 wallpapers" href="http://xnys.blogspot.co.uk/2012/12/happy-new-year-2013-wallpaper.html" target="_blank">web site</a>. Thank you XeBii Mj.</li>
</ul>
</li>
<li>Les Naufragés d&#8217;hytaq, Christophe Arleston / Adrien Floch / CrazyToons / Claude Guth / Sébastien Lamirand
<ul>
<li>Wikipedia, <a title="Wikipedia" href="http://fr.wikipedia.org/wiki/Les_Naufrag%C3%A9s_d'Ythaq" target="_blank">Les Naufragés d&#8217;Ythaq</a>.</li>
<li><a title="Ythaq" href="http://www.ythaq.com/" target="_blank">Ythaq</a> web site.</li>
<li><a title="Amazon FR" href="http://www.amazon.fr/Les-Naufrag%C3%A9s-dYthaq-Tome-incognita/dp/2845659024" target="_blank">Amazon.fr</a> .</li>
<li>Amazon UK, <a title="Amazon UK" href="http://www.amazon.co.uk/Soleil-Ythaq-Forsaken-World-Premiere/dp/0785136266/ref=pd_sim_b_1" target="_blank">&#8220;Ythaq &#8211; The Forsaken World &#8220;</a> .</li>
</ul>
</li>
<li>Testing Quadrants:
<ul>
<li><a href="http://blog.jonasbandi.net/2010/02/agile-testing-quadrants.html">http://blog.jonasbandi.net/2010/02/agile-testing-quadrants.html</a></li>
<li><a href="http://www.betterprojects.net/2010/08/lisa-crispins-test-quadrants.html">http://www.betterprojects.net/2010/08/lisa-crispins-test-quadrants.html</a></li>
<li><a href="http://www.agiletester.ca/downloads/Chapter__9x_Quadrant_Summary_v3.pdf">http://www.agiletester.ca/downloads/Chapter__9x_Quadrant_Summary_v3.pdf</a></li>
<li><a href="http://blog.jonasbandi.net/2010/08/cassandra-syndrome-knowing-that-failure.html">http://blog.jonasbandi.net/2010/08/cassandra-syndrome-knowing-that-failure.html</a></li>
<li><a title="Book at Amzon UK" href="http://www.amazon.co.uk/Agile-Testing-Practical-Addison-Wesley-Signature/dp/0321534468" target="_blank">The book at Amzon UK</a> .</li>
</ul>
</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/711/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/711/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=711&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2013/01/07/20-12-2012-has-past-2013-is-here-whats-next/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/happynewyear2013_small.png" medium="image">
			<media:title type="html">HappyNewYear2013</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/lesnaufragesdythaq_small.png" medium="image">
			<media:title type="html">Les Naufrages d&#039;Ythaq</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/agiletestingquadrants_small.png" medium="image">
			<media:title type="html">Testing Quadrants</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/googletoyataselfdrivingcar_small.png" medium="image">
			<media:title type="html">Google/Toyata Self-driving car</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/ibm5in5_small.png" medium="image">
			<media:title type="html">IBM5in5_small</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/microsoftphoneontheskin_small.png" medium="image">
			<media:title type="html">MicrosoftPhoneOnTheSkin_small</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2013/01/bendablephones_small.png" medium="image">
			<media:title type="html">BendablePhones_small</media:title>
		</media:content>
	</item>
		<item>
		<title>IDisposable for dummies #2 &#8211; A guide about &#8216;how to implement it&#8217;</title>
		<link>http://blog.ilab8.com/2012/04/29/idisposable-for-dummies-2-how/</link>
		<comments>http://blog.ilab8.com/2012/04/29/idisposable-for-dummies-2-how/#comments</comments>
		<pubDate>Sun, 29 Apr 2012 18:28:58 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=126</guid>
		<description><![CDATA[In my previous post, I classified the different memory resources available in the .NET CLR and I explained the role of the IDisposable interface as well as the use of the Finalize() method. In this post, I am going to talk more about the implementation details of IDisposable and Finalize(). For reference, I have split [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=126&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2012/04/idisposablelogo-part2.png"><img class="alignleft size-full wp-image-656" title="IDisposable - Part 2" src="http://ilab8.files.wordpress.com/2012/04/idisposablelogo-part2.png?w=600" alt="IDisposable - How ?"   /></a>In <a title="IDisposable for dummies #1 – Why? What?" href="http://blog.ilab8.com/2012/04/26/idisposable-for-dummies-1-why/" target="_blank">my previous post</a>, I classified the different memory resources available in the .NET CLR and I explained the role of the<strong> IDisposable</strong> interface as well as the use of the <strong>Finalize()</strong> method.</p>
<p>In this post, I am going to talk more about the <strong>implementation details</strong> of IDisposable and Finalize().</p>
<p>For reference, I have split this matter into two posts:</p>
<ul>
<li><a title="IDisposable for dummies #1 – Why? What?" href="http://blog.ilab8.com/2012/04/26/idisposable-for-dummies-1-why/" target="_blank"># 1 &#8211; What + Why + references</a></li>
<li>#2 &#8211; A guide about &#8216;how to implement it&#8217; + samples (<span style="color:#ff9900;"><em>this post</em></span>)</li>
</ul>
<p><span id="more-126"></span></p>
<p>You should know by now<span style="text-decoration:underline;"><em> when</em></span> and <span style="text-decoration:underline;"><em>why</em></span> you should implement IDisposable and Finalize() method according to the type of resource(s) your class owns. In order to answer the question &#8220;How?&#8221; we still do need to look at two more aspects: 1) Inheritance (as in Object-Oriented Programming) &amp; 2) Some remaining considerations.</p>
<h2>OOP &#8211; Inheritance</h2>
<p>There is one more aspect to take into consideration in object-oriented programming and that is inheritance. When you define a public base class (or any public class that can be instantiated and is not marked as &#8220;sealed&#8221;) you should consider that even though you have not any managed or unmanaged resources, you might still need to think about the potential classes that would derived from it. They might require managed or unmanaged resources. In that case, you should make the finalization code (IDisposable + Finalize()) in your base class, so that it is part of the contract and derived-class or client-code could use it correctly (i.e. calling Dispose() whenever it is required).</p>
<p>Obviously, if you are SURE to be  in control of ALL the client code of your class, then you can wait for your derived classes to really require this resource management code (IDisposable + Finalize()) before implementing it.</p>
<p>As for me, as soon as I create a new class, my first step is to make it &#8220;sealed&#8221; &#8211; I found this &#8220;habit&#8221; very effective, because it closes a lot of questions about inheritance (which method must be declared &#8220;virtual&#8221;, which method/properties/field should be declared &#8220;protected&#8221;, &#8230;). If later the class must become a base class, then I can review the changes and needs at that time. This is my way, everyone is different <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2> Some remaining considerations</h2>
<h4>Finalizers:</h4>
<ul>
<li>Be careful, if an exception is thrown in the constructor, the finalizer code will still be executed, so make sure to check whether a field (to a native resource) has been correctly initialized before calling any clean up code on it.</li>
</ul>
<h4>Dispose() method:</h4>
<ul>
<li>To help to ensure that resources are always cleaned up appropriately, a Dispose() method <strong>should be callable multiple times</strong> <strong>without throwing an exception</strong>.</li>
<li>The object must dispose all of the resources it owns.</li>
<li>A disposed object cannot be used anymore (i.e. once its Dispose() method has been called), so it must become &#8221;garbage&#8221; so next time the garbage collector will kick off its managed memory can be reclaimed (or if it has a Finalizer(), it will be place on the &#8220;f-reachable&#8221; list).</li>
<li>Once an object has been disposed, all of its public methods must throw the exception of type <strong>&#8220;ObjectDisposedException&#8221;</strong>.</li>
<li>You must make sure that no delegates (from Rooted object) are pointing to it, usually done via &#8220;<em><strong>events</strong></em>&#8220;. Here, you have two choices:
<ol>
<li>The instance which is running Dispose() exposes event(s) (aka source of event(s)), then it must reset the list of its subscribers (you might want to define a &#8216;OnDisposing&#8217; event, so that client code might register and have a chance to be notified when Dispose() is called on the source).</li>
<li> The instance which is running Dispose() consumes event(s) (aka subscriber of event(s)), then it must unsubscribe itself from the source(s) of events.</li>
</ol>
</li>
</ul>
<h4>Versioning:</h4>
<ul>
<li>You have to be careful when adding &#8220;IDisposable&#8221; to your class.
<ul>
<li>If you are in control of all the assemblies using it, then you can add it and review its usage amongst all your code.</li>
<li>If you have shipped or distributed your class to customers/partners, you cannot add it like that, because this change the semantics of your class and the client code is not aware of it. In that case, one option to make your existing class &#8220;Obsolete&#8221; and create a new one with the implementation of IDisposable and let your clients know about the new class.</li>
</ul>
</li>
</ul>
<h4>Native Handle</h4>
<ul>
<li>Since .NET 2.0, Microsoft has released a new abstract class &#8216;<a title="MSDN, SafeHanlde class" href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle(v=vs.100).aspx" target="_blank">SafeHandle</a>&#8216; [Ref-01 &amp; 02] that can help you in disposing native handles. Most of the .NET classes that wrap native handles (File, Pipe, Wait handle, Registry key, &#8230;) use internally specialized classes derived from &#8216;SafeHandle&#8217; (<em>read the doc on .NET v4.0 or above of MSDN about this subject</em>). So you might consider, if needed, to create your own SafeHandle derived class for disposing native handles.</li>
</ul>
<h2>Decision Matrix</h2>
<p>So, now we have all the bits of information that should be taken into consideration to find out what really is needed in the code for your class:</p>
<ul>
<li><em><strong>Sealed class</strong></em> or <em><strong>base class</strong></em>?</li>
<li>What <em><strong>type of resources</strong> </em>does <em><strong>your class own</strong></em>?</li>
</ul>
<p>Once you answered to those questions, then the matrix below provides you with the bits that are required for you case:</p>
<p><a href="http://ilab8.files.wordpress.com/2012/04/idispose-thedecisionmatrix.png"><img class="aligncenter size-full wp-image-634" title="IDispose - The Decision Matrix" src="http://ilab8.files.wordpress.com/2012/04/idispose-thedecisionmatrix.png?w=600&#038;h=251" alt="" width="600" height="251" /></a></p>
<p><em><span style="text-decoration:underline;">Nota:</span></em></p>
<ul>
<li><em><strong>Finalize():</strong></em>
<ul>
<li>Must only release unmanaged resources. E.g. native/unsafe handles on file, windows etc.<br />
No cleaning code on .NET object field(s) should be done inside a Finalizer. (i.e. calling Dispose(), Close() or removing event handlers &#8230;).</li>
</ul>
</li>
<li><em><strong>Dispose(bool):</strong></em> implement it when the release logic is shared by Dispose() and Finalizer()</li>
</ul>
<p>Here is a link for downloading a <a title="Sample solution - Renamed to '.zip'" href="http://ilab8.files.wordpress.com/2012/04/idisposablesamples-zip-toberenamed.doc" target="_blank">sample solution</a> (VS 2010, C# source code).</p>
<p>The sample code I am providing respect the decision matrix above (and is deliver &#8220;AS -IS&#8221;, the usual formula for &#8220;disclaim&#8221;). However, because of refactoring and reuse of code you will find some differences. E.g. for &#8220;non-sealed class&#8221; instead of defining a &#8220;protected virtual Dispose(bool)&#8221; method I instead defined a &#8220;private&#8221; one with the hard-coded &#8221;Dispose(bool) logic&#8221; in it and defined two virtual methods: one for disposing <em>Managed Resources</em> and one for <em>Unmanaged Resources</em> (<em>I did not update the decision matrix, because it is close to what you can find on current MSDN documentation, so most developers should be familiar with it &#8211; the principles are here</em>).</p>
<h2>Fx-Cop to the rescue</h2>
<p>If you are using Visual Studio 2005 and above and have the “FX-Cop” as part of your edition (also referred as “Code Analysis”) it is a good practice to enable the following checking rules:</p>
<p><a href="http://ilab8.files.wordpress.com/2012/04/fx-cop_rules.png"><img class="aligncenter size-full wp-image-631" title="Fx-Cop - Rules" src="http://ilab8.files.wordpress.com/2012/04/fx-cop_rules.png?w=600&#038;h=131" alt="" width="600" height="131" /></a></p>
<p>This way, every time you add code to your project, FX-Cop can look for you for new usage of Disposable resources and warn you about your usage in your code. This is not bullet proof (I had some cases, long chain of inheritance, where FX-Cop did not detect and did not warn about the not use of IDisposable.Dispose() &#8211; but this was more the exception than the rule, so use it).</p>
<h2>Conclusion</h2>
<p>That is it for me. I hope you have now a better understanding about &#8220;how to implement IDisposable&#8221;.</p>
<h2><span style="text-decoration:underline;">References:</span></h2>
<ul>
<li>[REF-01] &#8221;<a title="MSDN, Safe Handles and Critical Finalization" href="http://msdn.microsoft.com/en-us/library/fh21e17c(v=vs.110).aspx" target="_blank">Safe Handles and Critical Finalization</a>&#8220;, MSDN online.</li>
<li>[REF-02] &#8220;<a title="MSDN, SafeHandle class" href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle(v=vs.100).aspx" target="_blank">SafeHandle Class</a>&#8220;, MSDN online.</li>
<li>&#8220;<a title="Microsoft.Win32.SafeHandles Namespace" href="http://msdn.microsoft.com/en-us/library/microsoft.win32.safehandles.aspx" target="_blank">Microsoft.Win32.SafeHandles Namespace</a>&#8220;, MSDN online (<em>from Donald&#8217;s comment</em>)</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/126/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/126/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=126&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/04/29/idisposable-for-dummies-2-how/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/04/idisposablelogo-part2.png" medium="image">
			<media:title type="html">IDisposable - Part 2</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/04/idispose-thedecisionmatrix.png" medium="image">
			<media:title type="html">IDispose - The Decision Matrix</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/04/fx-cop_rules.png" medium="image">
			<media:title type="html">Fx-Cop - Rules</media:title>
		</media:content>
	</item>
		<item>
		<title>IDisposable for dummies #1 &#8211; Why? What?</title>
		<link>http://blog.ilab8.com/2012/04/26/idisposable-for-dummies-1-why/</link>
		<comments>http://blog.ilab8.com/2012/04/26/idisposable-for-dummies-1-why/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 22:50:00 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=100</guid>
		<description><![CDATA[(It took me more than a year to finish writing this article &#8211; a &#8220;hot topic&#8221; and I wanted it to be clear, simple and right. Also, I was busy and did not want to publish anything &#8220;unfinished&#8221;. As usual, your feedback and comments are more than welcome. Thank you in advance) Recently (February 2011), I had to review [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=100&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>(<a href="http://ilab8.files.wordpress.com/2011/08/idisposablelogo-part1.png"><img class="alignleft size-full wp-image-641" title="IDisposable - Part 1" src="http://ilab8.files.wordpress.com/2011/08/idisposablelogo-part1.png?w=600" alt="IDisposable - What ? Why?"   /></a>It took me more than a year to finish writing this article &#8211; a &#8220;hot topic&#8221; and I wanted it to be clear, simple and right. Also, I was busy and did not want to publish anything &#8220;unfinished&#8221;. As usual, your feedback and comments are more than welcome. Thank you in advance)</em></p>
<p>Recently (February 2011), I had to review some .NET code and came across of some wrong implementations of IDisposable.</p>
<p>After discussing with the developers, there were many reasons for what I found, ranging from:</p>
<ul>
<li>Not knowing the difference between a &#8220;CLR memory resource&#8221;, a &#8220;managed resource&#8221; and an &#8220;unmanaged resource&#8221;;</li>
<li>Not understanding &#8220;how and when&#8221;  resources are &#8220;released&#8221;;</li>
<li>Not knowing when to override the &#8220;Finalize()&#8221; method and what should be released by a finalizer?</li>
</ul>
<p><em><span id="more-100"></span>Even if the recent publications are becoming better, documentation from Microsoft is not fully clear on this subject. There are still some ambiguous areas about when you should implement a Finalizer (I must admit, since I started to do .NET programming back in 2002 (with .NET 1.1), MSDN documentation has improved a lot, but it took me a lot of time to understand this topic &#8211; thanks to the books like the ones from Jeffrey Richter [REF-01], Don Box [REF-02] and Bill Wagner [REF-03]).</em></p>
<p>I have split this matter into two posts :</p>
<ul>
<li># 1 &#8211; What + Why + references (<span style="color:#ff6600;">this post</span>)</li>
<li><a title="IDisposable for dummies #2 – A guide about ‘how to implement it’" href="http://blog.ilab8.com/2012/04/29/idisposable-for-dummies-2-how/" target="_blank"><em>#2 &#8211; A guide about &#8216;</em>how to implement it&#8217; + samples</a></li>
</ul>
<p>If you already know about the IDisposable pattern and what are the types of resources managed by the .NET CLR (Common Language Runtime &#8211; Microsoft implementation of the managed runtime environment on the .NET platform), please go directly to <a title="IDisposable for dummies #2 – A guide about ‘how to implement it’" href="http://blog.ilab8.com/2012/04/29/idisposable-for-nuts-2-how/" target="_blank">the second post</a>. If you do not know or want to learn more about it, please keep reading.</p>
<h2>What are memory resources on .NET CLR?</h2>
<p>In managed environments (like the .NET CLR or the Java JVM), &#8220;memory management on the heap&#8221; is taken care by the<strong> runtime engine</strong>, i.e. <strong>CLR</strong> on the .NET platform &#8211; The rest of the discussion will focus only on the  .NET platform.</p>
<p>Like a supervisor, the runtime engine knows everything about heap memory allocation in its world, aka the <strong>managed environment</strong>. However, this managed environment lives and is hosted in a less &#8220;managed&#8221; environment, e.g. the Windows operating system.  Your application might need some resources that are not managed by the CLR and even living outside your process. They could be locally allocated on the same machine or remotely far away on another machine. The important point is that you can use them from your code in .NET.</p>
<p><a href="http://ilab8.files.wordpress.com/2011/01/idisposable-memoryobjects1.png"><img class="aligncenter size-full wp-image-149" title="IDisposable.MemoryObjects" src="http://ilab8.files.wordpress.com/2011/01/idisposable-memoryobjects1.png?w=600" alt=""   /></a></p>
<p>From this picture we can see 3 types of memory resources for a .NET application:</p>
<ul>
<li><span style="color:#0000ff;"><strong>CLR memory resources</strong></span>, such as <span style="color:#0000ff;"><em>_myNetObj</em></span> or the <span style="color:#0000ff;">string <em>&#8220;Hello world!&#8221;</em></span>. Their memory space and fields (<span style="color:#0000ff;">int</span> and <span style="color:#0000ff;">string</span>) are all allocated on the CLR&#8217;s heap memory.</li>
<li><span style="color:#008000;"><strong>Managed resources</strong></span>, such as <span style="color:#008000;">_myObjWindow, _myObjFile, _timer1</span>, <span style="color:#008000;">_file1</span>, <span style="color:#008000;">_connection1</span>. They are .NET classes which have <span style="color:#ff0000;"><em>direct unmanaged fields</em></span> and/or <span style="color:#008000;"><em>other managed resource as fields</em></span>. Their memory space is managed by the CLR.</li>
<li><span style="color:#ff0000;"><strong>Unmanaged resources</strong></span>, such as <em><span style="color:#ff0000;">window, timer, file, DB</span></em>. They live outside the CLR&#8217;s heap memory. They can be referred as &#8220;<span style="color:#ff0000;"><strong>native resources</strong></span>&#8221; as well.</li>
</ul>
<p>The CLR only controls and manages its memory. The CLR has its own algorithms to allocate/move/release its memory (heap, large object heap, &#8230;). The CLR&#8217;s heap memory management is based on a &#8220;generational garbage collection&#8221;.  I am not digging too much in details inside the CLR implementation of its &#8220;garbage collector&#8221;, because it would require many posts <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but the aim of the garbage collector is to reclaim <em><strong>not reachable managed memory</strong></em> and <em><strong>re-compact the memory</strong></em> to <em><strong>prevent memory fragmentation</strong></em> as much as possible. For further details, please see [REF-01<strong><em></em></strong>] [REF-07] and [REF-08].</p>
<p>The <strong>bold</strong> statement above requires a shift of mind from the way development is done in a &#8220;native memory management&#8221;, such as in C and C++ language, compare to a managed environment like the .NET CLR.</p>
<p>In native memory management, the developer that you are is in charge of the memory allocation, you will do a malloc() (or new()) method to allocate memory, use your object and when you do not need it anymore you will call de-allocate() (or delete()) method to free the memory. You have &#8220;great powers&#8221;, but also great responsibilities about the management of your memory. This style of memory management is also referred as &#8220;<strong>deterministic memory management</strong>&#8220;. By opposition, CLR&#8217;s style is referred as &#8220;<strong>non-deterministic memory management</strong>&#8220;.</p>
<p>In the &#8220;<strong>deterministic</strong> memory management&#8221; style, there are a few drawbacks:</p>
<ul>
<li>First you end up managing handlers/pointers to memory area. You have to be careful and aware of the size of the &#8220;right portion of the  memory allocated&#8221; when writing and reading from it.
<ul>
<li>Overrun buffer, stack data corrupted, memory corrupted are results of misuse of those handlers/pointers.</li>
</ul>
</li>
<li>For some applications, the repetition of the cycle  &#8220;allocation/de-allocation&#8221; on a native heap space will fragment the memory and the process will end up at some point with an &#8220;out-of-memory&#8221; error, even though it still has a lot of free space (the free space is <em>fragmented</em> and not <em>contiguous</em>).</li>
<li>From a security point of view, the system cannot verify the stack and its memory pointers and apply security checks/rules.</li>
</ul>
<p>To address some of those issues, managed environments (which have existed since the 70s, &#8220;hello Smalltalk !&#8221;) have been revisited and refreshed with the latest features. For the CLR, this includes the following features (not a thorough list):  &#8220;<strong>generational</strong> garbage collectors&#8221; and &#8220;Code Access Security (CAS) for .NET&#8221;.</p>
<p>Now, in this new managed environment world, the developers are not concerns anymore about memory allocation/de-allocation of <em><span style="color:#0000ff;"><strong>CLR memory resources</strong></span></em>. However, as your application does not lived (yet!) in a full managed operating system, you must pay attention to <span style="color:#008000;"><strong><em>managed resources</em></strong> <span style="color:#000000;">and</span></span><strong><em><span style="color:#ff0000;"> unmanaged resources</span></em></strong>.</p>
<p>The other key point to retain is that <em><strong>once you have finished using an object allocated on the CLR&#8217;s heap it might be de-allocated at some point but nothing is certain and there is no guaranty (i.e. about &#8220;when&#8221; and whether it would be de-allocated at all).</strong></em></p>
<p>In fact, in the &#8220;<strong>non-deterministic</strong> memory management&#8221; style you have:</p>
<ul>
<li>Not sure the CLR will re-use the memory space used be your object (if there is no need to claim memory, why bother).</li>
<li>You do not know when this space will be re-used.</li>
</ul>
<p>That leaves the .NET developer with 2 problems:</p>
<ol>
<li>How can he &#8220;force/trigger&#8221; the release of resources?</li>
<li>How can he make sure that <strong><span style="color:#008000;">managed resources</span></strong> and <strong><span style="color:#ff0000;">unmanaged resources</span></strong> will still be released (at some point)?</li>
</ol>
<p>To help you with that task about releasing other resources than <em><span style="color:#0000ff;">CLR memory resources</span></em>, the .NET team came up with the <span style="color:#008000;"><strong>IDisposable interface</strong></span> [Ref-04]  and the <strong><span style="color:#008000;">Dispose pattern</span></strong> [Ref-05]. The other .NET feature that will help you is the<strong><span style="color:#ff0000;"> Finalize()</span></strong> method (a protected method define at the <strong>System.Object</strong> level).</p>
<h2>What about IDisposable ?</h2>
<p>.NET objects live in a managed world (aka CLR). We know that the CLR memory management style is <span style="color:#0000ff;">non-deterministic</span>, i.e. developers do not have to worry about releasing the CLR heap memory allocated by <span style="color:#0000ff;"><em>CLR memory resources</em></span>. However, for other types,<span style="color:#008000;"><strong><em> managed resources</em></strong></span> and<span style="color:#ff0000;"><strong><em> unmanaged resources</em></strong></span>, we do not want those objects to stay in memory for long after we have finished to use them. Some of those objects have a <em>Close()</em> method, some have a <em>Free()</em> method, some have a <em>Release()</em> method, and some have even a mix of them.</p>
<p>So, how can I (as a developer) know which one to call to free up the managed and native resources? (like DB connections, files, &#8230;)</p>
<p>The .NET team came up with a single interface named <strong><span style="color:#008000;">IDisposable</span></strong> with a single method called<strong><span style="color:#008000;"> Dispose()</span></strong> to standardize the way resources are disposed:</p>
<ul>
<li>Because it is an interface, it does not prevent your class to inherit implementation from whatever class it needs to (remember, you are only allow one implementation class inheritance, but many interface inheritances);</li>
<li>As you decorate your class with that interface, it adds semantics (interfaces are like contracts) and intention to your class. So, developers using your class should know about it.</li>
<li>The dispose method is simple &#8211; no parameters &#8211; and its name is clear: dispose any resources you have.</li>
<li>Developers should only be aware if a class Implements IDisposable to call a single method, <strong>Dispose()</strong>, to release all resources by an instance of that class.</li>
</ul>
<p>The .NET C# team went a step further by providing a construct in the language called &#8220;<strong>using</strong>&#8221; that will allow you to declare, use and call the <strong>Dispose()</strong> method in a <strong>try/finally block</strong>.</p>
<h2>Why do you a need a Finalizer?</h2>
<p>You only need to implement  a Finalizer (<em>i.e. override <strong>Finalize()</strong> method</em>) in your class if <em><strong>your class <span style="text-decoration:underline;">directly </span>creates a <span style="color:#ff0000;">native resource</span></strong></em> (aka <strong><em>unmanaged resource</em></strong>) like a memory space in the unmanaged heap, a GDI handle or a file handle.</p>
<p>Otherwise, if your class is using the .NET objects provided by the .NET framework (e.g.System.IO. Stream), those classes are <span style="color:#008000;"><em><strong>managed resources</strong></em></span> and they already implement <strong>IDisposable</strong> and have a <strong>Finalize()</strong> method overridden. So, you only need to call their Dispose() method from your implementation. <strong>YOU DO NOT HAVE TO implement a Finalizer() in that case</strong>.</p>
<p>Jeffrey Ritcher [Ref-01] has some other examples that you might want to implement a Finalizer() in your class even though your class does not have a (direct) native resource (e.g. if you want to monitor garbage collection). I do think this is the exception to the rule.</p>
<p>In the &#8220;non-deterministic memory management&#8221; style the finalization is as follows:</p>
<ul>
<li>Exact time of finalization is unspecified. (When? =&gt; you do not know)</li>
<li>Order of finalization is unspecified. (Order? =&gt; you do not know)</li>
<li>Thread running the <strong>Finalize()</strong> method is unspecified. (Which Thread? =&gt; you do not know)</li>
</ul>
<p>Objects with a <strong>Finalize()</strong> method overridden have an extra cost associated with them in order to reclaim the CLR memory used by your object [REF-01][REF-06][REF-07][REF-08] . It will take at <em>least two</em> garbage collections:</p>
<ul>
<li>First one to put your object in the &#8220;f-reachable&#8221; list;</li>
<li>Run your Finalizer() code</li>
<li>Then remove your object from the &#8220;f-reachable&#8221; list, so that your object becomes unreachable (i.e. no roots referencing it);</li>
<li>Its managed heap memory will be reclaimed in the <strong>next full garbage collection</strong> (i.e. generation 2, current maximum generation level in the CLR as I am writing these lines).</li>
</ul>
<p>The .NET C# team provided a <em><span style="color:#ff0000;">destructor syntax</span></em> (<em><strong>only the syntax, not the semantics !!</strong></em>), but the piece of code written in that method will be placed inside the <strong>Finalize()</strong> method by the C# compiler in a try/finally block, with a call to the base class in the finally block. This is maybe one of the ambiguous area and syntax for C# developers to understand: <em><strong>the Finalize() method is not a destructor</strong></em>, i.e. like in C++.</p>
<p>If your class has one (or more) unmanaged resource then you will need to:</p>
<ul>
<li>Override the <strong>Finalize()</strong> method;</li>
<li>Call GC.SuppressFinalize() from your public <strong>Dispose()</strong> method;</li>
<li>Your Dispose() method must release any managed and unmanaged resources;</li>
<li>Your Dispose() method can be called multiple times without failing.</li>
<li>Once your object has been disposed, it cannot be (re-)used anymore. It is garbage !!</li>
<li>Once your object has been disposed, all public instance methods should raise the exception &#8220;ObjectDisposedException&#8221;.</li>
<li>Only release unmanaged resources in your Finalize() method.</li>
</ul>
<p>Please, refer to the second post, there is a detailed matrix about what needs to be implemented in various scenarios and some sample code.</p>
<h2>End of part 1</h2>
<p>I hope that by now you have a better understanding about:</p>
<ul>
<li>The different types of memory resources available in the CLR (<strong><span style="color:#0000ff;">CLR memory resource</span></strong>, <strong><span style="color:#008000;">managed resource</span></strong> and <strong><span style="color:#ff0000;">unmanaged resource</span></strong>).</li>
<li>The IDisposable interface.</li>
<li>The Finalize() method and when to implement it.</li>
</ul>
<p><a title="IDisposable for dummies #2 – A guide about ‘how to implement it’" href="http://blog.ilab8.com/2012/04/29/idisposable-for-dummies-2-how/" target="_blank">My second post</a> has a detailed matrix to help you find out, depending in your context, what needs to be implemented and some code samples.</p>
<h2><span style="text-decoration:underline;">References:</span></h2>
<ul>
<li>[REF-01] Jeffrey Richter, CLR via C#, 3rd Edition, Microsoft Press (see Chapter 21).</li>
<li>[REF-02] Don Box and Chris Sells, Essential .NET &#8211; Volume 1 (p. 147-152).</li>
<li>[REF-03] &#8220;Effective C#&#8221;, Bill Wagner, second edition, Addison Wesley (Ch2 p.69-74; item-17)</li>
<li>[REF-04] &#8220;<a href="http://msdn.microsoft.com/en-us/library/system.idisposable%28VS.90%29.aspx">IDisposable Interface</a>&#8220;, MSDN online</li>
<li>[REF-05] &#8220;<a href="http://msdn.microsoft.com/en-us/library/fs2xkftw%28v=VS.90%29.aspx">Implementing a Dispose Method</a>&#8220;, MSDN online</li>
<li>[REF-06] &#8220;<a href="http://msdn.microsoft.com/en-us/library/aa720079%28v=vs.71%29.aspx" target="_blank">Finalize Methods and Destructors</a>&#8220;, MSDN online</li>
<li>[REF-07] &#8220;<a title="MSDN Magazine, November 2000" href="http://msdn.microsoft.com/en-us/magazine/bb985010.aspx" target="_blank">Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework (Part-1)</a>&#8220;, Jeffrey Richter, MSDN November 2000.</li>
<li>[REF-08] &#8220;<a title="MSDN Magazine, December 2000" href="http://msdn.microsoft.com/en-us/magazine/bb985011.aspx" target="_blank">Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework (Part-2)</a>&#8220;, Jeffrey Richter, MSDN December 2000.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/100/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/100/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=100&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/04/26/idisposable-for-dummies-1-why/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/08/idisposablelogo-part1.png" medium="image">
			<media:title type="html">IDisposable - Part 1</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/01/idisposable-memoryobjects1.png" medium="image">
			<media:title type="html">IDisposable.MemoryObjects</media:title>
		</media:content>
	</item>
		<item>
		<title>Windows 8 &#8211; Not that easy to uninstall programs &#8230;</title>
		<link>http://blog.ilab8.com/2012/03/03/windows-8-not-that-easy-to-uninstall-programs/</link>
		<comments>http://blog.ilab8.com/2012/03/03/windows-8-not-that-easy-to-uninstall-programs/#comments</comments>
		<pubDate>Sat, 03 Mar 2012 20:16:41 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[Computers and Internet]]></category>
		<category><![CDATA[Windows 8]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=577</guid>
		<description><![CDATA[UPDATE &#8211; 27th October 2012 - THIS WAS DONE ON THE WINDOWS 8 CONSUMER PREVIEW, BACK IN FEBRUARY 2012. NOT ON THE RECENT PUBLIC RELEASE OF WINDOWS 8 (OCTOBER 2012). THINGS HAVE IMPROVED SINCE. Good news, Microsoft just put out last Wednesday, 29th February 2012, their new &#8221;Windows 8 - Consumer Preview&#8221; [Ref 01]. I had the &#8220;Developer Preview&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=577&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2012/03/logo_windows8-touch-the-future1.png"><img class="size-full wp-image-597 alignleft" style="border:3px solid orange;" title="logo_windows8-Touch the future" alt="" src="http://ilab8.files.wordpress.com/2012/03/logo_windows8-touch-the-future1.png?w=600"   /></a><strong><span style="color:#ff6600;">UPDATE &#8211; 27th October 2012 -</span> </strong><em>THIS WAS DONE ON THE <strong>WINDOWS 8 CONSUMER PREVIEW, BACK IN FEBRUARY 2012</strong>. NOT ON THE RECENT PUBLIC RELEASE OF WINDOWS 8 (OCTOBER 2012). THINGS HAVE IMPROVED SINCE.</em></p>
<p>Good news, Microsoft just put out last Wednesday, 29th February 2012, their new &#8221;<strong>Windows 8 - Consumer Preview</strong>&#8221; <a title="Download for Developers" href="http://msdn.microsoft.com/en-us/windows/apps/br229516" target="_blank">[Ref 01]</a>. I had the &#8220;Developer Preview&#8221; on my beautiful tablet (gift from Samsung and Microsoft to the BUILD conference attendees last September, thank you!), but it was not that stable, so I have been patient and I was looking forward this new release.</p>
<p>So, I updated my tablet (I did not sell it like some did <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ) on Thursday 1st March and played with it for the last 2 days. I must say it is a lot more stable and it boots on my tablet in about 10 seconds : that&#8217;s a change !</p>
<p>I went to the &#8220;Appication Store&#8221; and started to download almost everything they had on the first page &#8211; it was free. Then, As I developer, I started to install Visual Studio 11 Beta from the link provided by &#8220;Microsoft App Dev Center page&#8221; <a title="Get Windows 8 - Consumer Preview" href="http://windows.microsoft.com/en-US/windows-8/download?ocid=W_MSC_W8P_DevCenter_MetroApps_EN-US" target="_blank">[Ref 02]</a>. It is only at the end of the installation that I noticed it was &#8220;Express edition&#8221; and not &#8220;Ultimate&#8221; or &#8220;Pro&#8221; edition. This was not the edition I wanted &#8211; sorry, nothing wrong with &#8220;Express&#8221; : it is a fantastic product and its is free, but I am used to &#8220;Pro&#8221; and &#8220;Ultimate&#8221;. So, after half-hour of installation I am reduced to unsinstall &#8220;Visual Studio 11 Beta Express Edition&#8221; and start again.</p>
<p><strong>Wait a minute</strong> ! <em><strong>Where is the &#8220;Add/Remove Programs&#8221; on Windows 8?<span id="more-577"></span></strong></em></p>
<p>I spent nearly 15 minutes to find out where &#8220;Add/Remove Programs&#8221; is located now. I hope this is something that Microsoft will improve. Here are the steps I had to do (I will talked about the gestures I did as I am running this on my tablet):</p>
<table border="1">
<tbody>
<tr>
<th>Step</th>
<th>Description</th>
</tr>
<tr>
<td>1</td>
<td>Press the &#8220;Start&#8221; button to be on the &#8220;Start&#8221; page of Windows 8.</td>
</tr>
<tr>
<td>2</td>
<td> Swipe your finger from the right side to display the &#8220;Charm bar&#8221;.<a href="http://ilab8.files.wordpress.com/2012/03/01-swipe-charmbar.png"><img class="size-full wp-image-583 aligncenter" title="01-swipe-CharmBar" alt="" src="http://ilab8.files.wordpress.com/2012/03/01-swipe-charmbar.png?w=600"   /></a></p>
<p style="text-align:center;"><a href="http://ilab8.files.wordpress.com/2012/03/02-charmbar.png"><img class="wp-image-584 aligncenter" title="02-CharmBar" alt="" src="http://ilab8.files.wordpress.com/2012/03/02-charmbar.png?w=448&#038;h=232" height="232" width="448" /></a></p>
</td>
</tr>
<tr>
<td>3</td>
<td> Press &#8220;Settings&#8221;, the &#8220;Charm bar&#8221; will disapear and another panel on the right side will be displayed:<a href="http://ilab8.files.wordpress.com/2012/03/03-startpanel.png"><img class="aligncenter size-full wp-image-588" title="03-StartPanel" alt="" src="http://ilab8.files.wordpress.com/2012/03/03-startpanel.png?w=600"   /></a></td>
</tr>
<tr>
<td>4</td>
<td> At the top, press on &#8220;Settings&#8221; (a second time).</td>
</tr>
<tr>
<td>5</td>
<td> The &#8220;Start Settings&#8221; panel will be displayed. Switch on the feature &#8220;Show administrative tools&#8221;.<a href="http://ilab8.files.wordpress.com/2012/03/04-start-settings-menu.png"><img class="aligncenter size-full wp-image-592" title="04-Start Settings Menu" alt="" src="http://ilab8.files.wordpress.com/2012/03/04-start-settings-menu.png?w=600"   /></a></td>
</tr>
<tr>
<td>6</td>
<td> Now, go back to the &#8220;start&#8221; page (Press button &#8220;Start&#8221;)</td>
</tr>
<tr>
<td>7</td>
<td> Slide your finger &#8220;from right to left&#8221; and go to the end.</td>
</tr>
<tr>
<td>8</td>
<td> Try to find the &#8220;System Configuration&#8221; tile. Press it.<a href="http://ilab8.files.wordpress.com/2012/03/05-systemconfigonstartpage.png"><img class="aligncenter size-full wp-image-593" title="05 - SystemConfigOnStartPage" alt="" src="http://ilab8.files.wordpress.com/2012/03/05-systemconfigonstartpage.png?w=600"   /></a></td>
</tr>
<tr>
<td>9</td>
<td> Windows will navigate to the &#8220;Old desktop App&#8221;, and a Window &#8220;System Configuration&#8221; will be displayed.</td>
</tr>
<tr>
<td>10</td>
<td> Go to the &#8220;Tools&#8221; tab and look for a tool named &#8220;Programs&#8221;. Select it and click on the button &#8220;Launch&#8221; (maybe you can double-click it, but I am not doing it on my tablet because my finger is too big <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and the regular windows buttons are small).<a href="http://ilab8.files.wordpress.com/2012/03/06-programstool.png"><img class="aligncenter size-full wp-image-594" title="06-ProgramsTool" alt="" src="http://ilab8.files.wordpress.com/2012/03/06-programstool.png?w=600"   /></a></td>
</tr>
<tr>
<td>11</td>
<td> Now you can select the program you want to uninstall &#8211; for me it was VS 11 Beta Express Edition.</p>
<ul>
<li>This list only contains the programs for &#8220;regular&#8221; windows, i.e. not designed and developed as &#8220;Metro-App style&#8221;.</li>
<li>For &#8220;Metro-App style&#8221; I am assuming you can uninstall them from the &#8220;Store&#8221; &#8211; but I have not yet tried.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>I hope this helps.</p>
<p>If like me you want to install VS 11 Beta Ultimate edition see <a title="VS 11 Beta Ultimate edition" href="http://www.microsoft.com/visualstudio/11/en-us/downloads" target="_blank">[Ref-03]</a>.</p>
<p>Happy &#8220;swiping&#8221; on the new Windows 8.</p>
<p><span style="text-decoration:underline;">References</span></p>
<ul>
<li>[Ref-01] Download <a title="Get Windows 8 - Consumer Preview" href="http://windows.microsoft.com/en-US/windows-8/download?ocid=W_MSC_W8P_DevCenter_MetroApps_EN-US" target="_blank">&#8220;Windows 8 &#8211; Consumer Preview&#8221;</a>.</li>
<li>[Ref-02] MSDN, Windows | Dev Center &#8211; Metro style apps, <a title="Download for Developers" href="http://msdn.microsoft.com/en-us/windows/apps/br229516" target="_blank">&#8220;Download for Developers&#8221;</a>.</li>
<li>[Ref-03] Download <a title="VS 11 Beta Ultimate edition" href="http://www.microsoft.com/visualstudio/11/en-us/downloads" target="_blank">Visual Studio 11 Beta Ultimate edition</a>.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/577/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/577/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=577&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/03/03/windows-8-not-that-easy-to-uninstall-programs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/logo_windows8-touch-the-future1.png" medium="image">
			<media:title type="html">logo_windows8-Touch the future</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/01-swipe-charmbar.png" medium="image">
			<media:title type="html">01-swipe-CharmBar</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/02-charmbar.png" medium="image">
			<media:title type="html">02-CharmBar</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/03-startpanel.png" medium="image">
			<media:title type="html">03-StartPanel</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/04-start-settings-menu.png" medium="image">
			<media:title type="html">04-Start Settings Menu</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/05-systemconfigonstartpage.png" medium="image">
			<media:title type="html">05 - SystemConfigOnStartPage</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/03/06-programstool.png" medium="image">
			<media:title type="html">06-ProgramsTool</media:title>
		</media:content>
	</item>
		<item>
		<title>CES 2012 &#8211; Your phone Smoked by Windows Phone? 88% yes</title>
		<link>http://blog.ilab8.com/2012/01/14/ces-2012-your-phone-smoked-by-windows-phone-88-yes/</link>
		<comments>http://blog.ilab8.com/2012/01/14/ces-2012-your-phone-smoked-by-windows-phone-88-yes/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 10:50:59 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=569</guid>
		<description><![CDATA[CES 2012 has just finished and I came across the challenge &#8220;SmokedByPhone&#8221; [REF-02]. You can watch the videos on YouTube [REF-03]. The challenge results are interesting, specially for a phone OS which have only about 1.5 % of the global phone shares market (see my previous entry on that). Overall results for Windows Phone 7.5 (aka [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=569&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2012/01/smokedbywindowsphone_small.png"><img class="alignleft  wp-image-570" style="border:5px solid white;" title="SmokedByWindowsPhone_small" src="http://ilab8.files.wordpress.com/2012/01/smokedbywindowsphone_small.png?w=285&#038;h=165" alt="Smoked by Windows Phone?" width="285" height="165" /></a>CES 2012 has just finished and I came across the challenge &#8220;SmokedByPhone&#8221; <a href="http://windowsteamblog.com/windows_phone/b/windowsphone/archive/2012/01/12/video-windows-phone-wins-88-of-smokedbywindowsphone-challenges-at-ces-2012.aspx" target="_blank">[REF-02]</a>.</p>
<p>You can watch the videos on YouTube <a title="YouTube Videos" href="http://www.youtube.com/watch?feature=player_embedded&amp;v=jz8d5NF_wFI" target="_blank">[REF-03]</a>. The challenge results are interesting, specially for a phone OS which have only about 1.5 % of the global phone shares market (see<a title="Windows Phone – Where are we at the end of 2011?" href="http://blog.ilab8.com/2011/12/31/windows-phone-where-are-we-at-the-end-of-2011/" target="_blank"> my previous entry on that</a>).</p>
<p>Overall results for Windows Phone 7.5 (aka Mango):</p>
<ul>
<li>30 wins.</li>
<li>3 loses</li>
<li>1 draw</li>
</ul>
<p>=&gt; 88% wins. Not bad !!</p>
<p>Let&#8217;s see by the end of 2012 where Windows Phone will be &#8211; Nokia and HTC launched this week new phones in USA, that should boost Windows Phone sales and shares market.<span id="more-569"></span></p>
<p>Ok, I might seem a bit too &#8220;Pro-Windows Phone&#8221;, but I <span style="text-decoration:underline;">do</span> think  that <span style="text-decoration:underline;">Microsoft has done a tremendous OS for the phones</span>, specially around the &#8220;people hub&#8221; for communicating with people and social networks. People should have a try at it before buying their new phone!</p>
<h4><span style="text-decoration:underline;"><strong>References:</strong></span></h4>
<ul>
<li>[REF-01] <a href="http://www.microsoft.com/presspass/events/ces/VideoGallery.aspx" target="_blank">Microsoft CES 2012 Gallery page</a> .</li>
<li>[REF-02] <a href="http://windowsteamblog.com/windows_phone/b/windowsphone/archive/2012/01/12/video-windows-phone-wins-88-of-smokedbywindowsphone-challenges-at-ces-2012.aspx" target="_blank">&#8220;Smoked By Windows Phone Challenge at CES 2012&#8243;</a>, by Ben Rudolph, Windows Phone Team blog, 12-Jan-2012.</li>
<li>[REF-03] <a title="You tube" href="http://www.youtube.com/watch?feature=player_embedded&amp;v=jz8d5NF_wFI" target="_blank">Videos on YouTube</a>.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/569/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=569&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/01/14/ces-2012-your-phone-smoked-by-windows-phone-88-yes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/01/smokedbywindowsphone_small.png" medium="image">
			<media:title type="html">SmokedByWindowsPhone_small</media:title>
		</media:content>
	</item>
		<item>
		<title>Exception &#8211; How to raise and retrieve error information whithout losing it?</title>
		<link>http://blog.ilab8.com/2012/01/13/exception-how-to-raise-and-retrieve-error-information-whithout-losing-it/</link>
		<comments>http://blog.ilab8.com/2012/01/13/exception-how-to-raise-and-retrieve-error-information-whithout-losing-it/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 20:30:56 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Troubleshooting]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=494</guid>
		<description><![CDATA[ All .NET applications at sometime will have to deal with an exception. It is just a matter of time. As application run longer in PRODUCTION phase than in development (unless it is a prototype ) the more information you can get out of the exception, the more likely you will be able to quickly pinpoint [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=494&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2011/12/exdemo_logo.png"><img class="alignleft size-full wp-image-513" title="ExDemo_logo" src="http://ilab8.files.wordpress.com/2011/12/exdemo_logo.png?w=600" alt="Small icon"   /></a> All .NET applications at sometime will have to deal with an exception. It is just a matter of time. As application run longer in PRODUCTION phase than in development (unless it is a prototype <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) the more information you can get out of the exception, the more likely you will be able to quickly pinpoint the root cause and resolve the issue.</p>
<p>I know, this might seem a little too basic, but there are still some misuses of Exception and misunderstanding about how to raise/re-raise exception without losing the original information. So here it is my contribution to help to improve the &#8220;good&#8221; use of Exception programming in .NET (samples are in C#, but the principles are valid for the .NET platform).<span id="more-494"></span></p>
<p><em>(Source code available here <a title="Visual Studio 2010 C# Samples" href="http://ilab8.files.wordpress.com/2011/12/exceptionsdemo-zip.doc">ExceptionsDemo.zip</a> &#8211; rename the file with a &#8220;.zip&#8221; extension)</em></p>
<h1>1 &#8211; The basics</h1>
<p>You can think of an Exception as a piece of data (with hopefully useful information) that once raised will disrupted the normal flow of function or application. By catching it, your code can then decide on whether or not to return to a normal flow, let it raise again or even add further information.</p>
<p>Exception can be raised in one place to indicate an unexpected failure or an abnormal use of a function/method. Then another piece of code would catch the exception and act upon it for various reasons (logging, audit, security, retry &#8230;.). The decoupling between the location that &#8217;raise&#8217; and the one that do the &#8216;catch&#8217; is for me the main added value of <strong>Exception programming</strong>.</p>
<p>In .NET, if an exception raises up to the CLR without finding a catch block that handles the exception, the default exception handler handles it and the process will be terminated.</p>
<p>The good things about Exception programming are:<br />
- The source (i.e. the location) raising the exception is decoupled from where it will be caught and act upon. It might be the direct callee method, or it could be the previous 100th call method on the stack.<br />
- Along the raising path, there might be more than one location to caught the exception and act on it.<br />
- The exception can be enriched along its raising path.</p>
<p>Andrew Pardoe <a title="Handling Corrupted State Exceptions" href="http://msdn.microsoft.com/en-us/magazine/dd419661.aspx" target="_blank">[Ref -03]</a> wrote a good article about the evolution from return codes to Exception programming.</p>
<p>I must also mention that Exception programming add a &#8220;little overhead&#8221; and could have in some circumstances a negative impact on performance (e.g. looping 1,000,000 with many try/catch embedded). It is up to you to test your code &#8211; normal path and abnormal path &#8211; to find out how your code react. However, as I said earlier, your code will run most of its time in the PRODUCTION environment, and to me having usuful information when facing a PRODUCTION issue has a bigger value that counter balance the &#8220;little overhead&#8221; issue.</p>
<p>In this blog I will present the different ways to raise, re-raise and catch exceptions.</p>
<p><a href="http://ilab8.files.wordpress.com/2011/12/exdemo_callschain.png"><img class="aligncenter size-full wp-image-517" title="ExDemo_CallsChain" src="http://ilab8.files.wordpress.com/2011/12/exdemo_callschain.png?w=600" alt="Chain of calls"   /></a></p>
<h2>1.1 &#8211; How to raise an exception?</h2>
<p>In .NET C#, you will raise an exception by using the keyword &#8216;throw&#8217;, like this:</p>
<pre class="brush: csharp; title: ; notranslate">
throw new InvalidOperationException(&quot;Zero cannot be a valid result for this implementation.&quot;);</pre>
<p>Or like this:</p>
<pre class="brush: csharp; title: ; notranslate">
   InvalidOperationException ex = new InvalidOperationException(&quot;Zero cannot be a valid result for this implementation.&quot;);
ex.Data.Add(&quot;m&quot;, m);
ex.Data.Add(&quot;n&quot;, n);
ex.Data.Add(&quot;result&quot;, result);
throw ex;
</pre>
<p>Raising the exception is the first step in providing useful information: a clear error message and the call stack.</p>
<p>The important thing to note is that the number of the line in the call stack will be the one where the exception was raised, i.e. where the call to &#8216;throw&#8217; is made (line #5) &#8211; not the number of line where the exception was instantiated (i.e. &#8216;new&#8230;&#8217; in line #1) - Line numbers in the call stack are only available if the &#8220;.pdb&#8221; (Program database files &#8211; you have to switch the compilation switch to produce those files in &#8216;Release&#8217; flavour &#8211; they are automatically generated in &#8216;Debug&#8217; flavour) files are deployed and available when the exception is raised.</p>
<p>In the last example above, the code is showing the use of the property &#8216;Data&#8217; to add further information. This is a good thing that application can use to transmit further information about the exception (paramerters, local value).  &#8217;Data&#8217; is a dictionary the old fashion way: Dictionary[object; object] (that was before the generics existed on .NET).  So you have to be becareful about:</p>
<ul>
<li>The type use for &#8216;key&#8217;. I usually use &#8216;string&#8217;, but as an object it could be anything.</li>
<li>The &#8216;key&#8217; must be unique, so if you use &#8216;string&#8217; there might be some naming collision. Usually, I use a name based on pattern like this: &lt;MyApp&gt;.&lt;MyClass&gt;.&lt;MyMethod&gt;.&lt;MyKeyName&gt;.</li>
<li>&#8216;Value&#8217; can be anything.</li>
</ul>
<p>As you will see further down, the exception can go through many exception handling blocks before being used, so you have to think about the data you want to store in &#8216;Data&#8217; and decide whether or not in your case storing sensitive data is fine or not. Also, if you put any reference to object(s) they will be directly accessible (via &#8216;Data&#8217;) by any code above your method, so maybe you should consider cloning your objects or only pass value types.</p>
<h2>1.2 &#8211; How to catch an exception?</h2>
<p>Using the try/catch block, you can catch an exception:</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
// some code that will raise an exception
}
catch (Exception ex) // Exception handling block
{
// You have access to the exception raised here;
}</pre>
<p>You can define more than one exception handling block. In that case, you must attention to the existing inheritance between the various exception classes you will be using for your handling blocks: define the more specialized exception class first, then the more generic ones.</p>
<p>E.g. from an inheritance point of view we have the following inheritance relationship for &#8216;ArgumentNullException&#8217;:</p>
<pre>System.<span style="color:#000000;">Object</span>
<span style="color:#000000;"> </span> System.<span style="color:#ff6600;">Exception</span>
    System.<span style="color:#339966;">SystemException</span>
      System.ArgumentException
        System.<span style="color:#3366ff;">ArgumentNullException</span></pre>
<p>In that case you should defined you handling block like that:</p>
<pre>            ...
            catch (<span style="color:#0000ff;">ArgumentNullException</span> nullEx)
            { ... }
            catch (<span style="color:#339966;">SystemException</span> sysEx)
            { ... }
            catch (<span style="color:#ff6600;">Exception</span> ex)
            { ... }</pre>
<h2>1.3 &#8211; What is available?</h2>
<p>The base class <span style="color:#ff6600;">System.Exception</span> [Ref-01] has defined some useful fields:</p>
<table border="1">
<tbody>
<tr>
<th>Property</th>
<th>Get/Set</th>
<th>Description</th>
</tr>
<tr>
<td>Data</td>
<td>Get</td>
<td>Gets a collection of key/value pairs that provide additional user-defined information about the exception.</td>
</tr>
<tr>
<td>InnerException</td>
<td>Get</td>
<td>Gets the Exception instance that caused the current exception.</td>
</tr>
<tr>
<td>Message</td>
<td>Get</td>
<td>Gets a message that describes the current exception.</td>
</tr>
<tr>
<td>Source</td>
<td>Get/Set</td>
<td>Gets or sets the name of the application or the object that causes the error.</td>
</tr>
<tr>
<td>StatckTrace</td>
<td>Get</td>
<td>Gets a string representation of the frames on the call stack at the time the current exception was thrown. This is even more useful if you have the pdb files deployed when the exception is raised.</td>
</tr>
<tr>
<td>TargetSite</td>
<td>Get</td>
<td>Gets the method that throws the current exception.</td>
</tr>
</tbody>
</table>
<p>Please note that some properties are not available on all the .NET Frameworks -  E.g. &#8216;Data&#8217; and &#8216;Source&#8217; are not available on the .NET Compact Framework. Please refer to the corresponding MSDN documentation for your version of .NET Framework.</p>
<h1>2 &#8211; What to do with the exception caught?</h1>
<p>At this stage, you caught an exception, you have mainly 3 choices:</p>
<ol>
<li>Do nothing.</li>
<li>Re-raise the exception
<ol>
<li>The original one + stack trace. Optionally, can add further info via &#8216;Data&#8217; property.</li>
<li>Reset the stack trace.</li>
</ol>
</li>
<li>Raise a new exception
<ol>
<li>Brand new one &#8211; lost info about the original</li>
<li>Use of InnerException</li>
</ol>
</li>
</ol>
<h2>2.1 &#8211; Do nothing (NOT RECOMMENDED, BUT SOMETIMES CANNOT DO OTHERWISE)</h2>
<p>At least you should log or trace the exception.<br />
In some occasions, you may have to refer to this case, i.e. ignore or swallow the exception raised. E.g., using a third-party assembly, the first call to their initialisation code was failing the first time and raising an exception. The second time it was working. While waiting for a fix from the editor, I implemented a catch() block that ignore the exception the first time (but logged it).</p>
<p>On other occasions, I have to refer to this case when writting another &#8216;try/catch&#8217; block within a &#8216;finally&#8217; block (see below for further details).</p>
<h2>2.2 How to re-raise an exception?</h2>
<p>During a chain of method calls, a piece of code might catch an exception to do some process on it (logging, audit, &#8230;), but does not want to stop the exception handling here. In that case, it must re-raise (or re-throw) the exception.<br />
There 2 possibilities:<br />
1) Re-raise the original Exception (with the original stack trace)<br />
2) Re-raise the original Exception and reset the call stack</p>
<h3>2.2.1 Re-raise as it was</h3>
<p>In this case we want to re-raise the exception as it was (line #8):</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
// some code that will raise an exception
}
catch (Exception ex)
{
// You have access to the exception raised here;
throw;
}</pre>
<p>In this case, just use the word, &#8220;throw;&#8221;. The original exception and its call stack will be preserved.</p>
<h3>2.2.2 Re-raise it by resetting the call stack &#8211; NOT RECOMMENDED</h3>
<p>In this case, you can re-throw the same exception like this (line #8):</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
                // some code that will raise an exception
}
catch (Exception ex)
{
// You have access to the exception raised here;
throw ex1; // Reset the call stack to this frame (i.e. this line of code becomes the bottom of the call stack !!!)
}</pre>
<p>I do not recommend this usage because the call stack is reset, so you will lose a lot of useful information during the support phase of your application:<br />
=&gt; You loose the original line of code that thrown the exception, and all the call chain.<br />
=&gt; The new line fo code in the call stack is the line #8 of &#8220;throw ex1;&#8221; &#8211; not so useful!</p>
<p>Also, I could not think of a valid scenario for this case &#8211; I do not even understand why the CLR allows this case &#8211; Please, I am curious, shared with me if you have a valid scenario for this one. As for me, I keep chasing down this usage on every project I worked on, because when it happens in PRODUCTION we will not have the original call stack.</p>
<h2>2.3 Raise a new exception</h2>
<p>Again here there are 2 possibilities:<br />
1) Raise a brand new exception and forget about the original one.<br />
2) Raise a new exception and set the InnerException field with the old one.</p>
<h3>2.3.1 Raise a brand new exception</h3>
<p>In this case we want to raise a fresh new exception and forget about the original one (line #8):</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
// some code that will raise an exception
}
catch (Exception ex)
{
// You have access to the exception raised here;
                throw new ArgumentException(&quot;Wrong Argument&quot;);
}</pre>
<p>This is sometimes necessary, e.g. when you want to prevent some sensitive data (server name, IP address, user info, &#8230;) to bubble up to the surface. Creating a brand new one with a general but useful message is a solution in that case.<br />
However, I will advise to still log the original one, e.g. in an encrypted log file if it is required, but log it.</p>
<h3>2.3.2 Raise a new exception and set the innerException field</h3>
<p>In this case, you want to add some information along side the original exception like this (setting the parameter name to &#8220;m&#8221; &#8211; line #8):</p>
<pre class="brush: csharp; title: ; notranslate">
try
{
// some code that will raise an exception
}
catch (Exception ex)
{
// You have access to the exception raised here;
                throw new ArgumentException(&quot;Wrong Argument&quot;, &quot;m&quot;, ex);
}</pre>
<p>This is fine, as the original exception is preserved with its information (mainly the call stack) and further information was added.</p>
<h1>3 &#8211; The default AppDomain exception handler</h1>
<p>The code you write in .NET will execute within an ApplicationDomain (System.AppDomain). This is the secure boundary context on the .NET platform to run code.  A process could have more than one AppDomain. The AppDomain has an event called &#8216;UnhandledException&#8217; <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" target="_blank">[Ref-02]</a> to which your code can regsiter a handler like this:</p>
<pre class="brush: csharp; title: ; notranslate">
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.Out.WriteLine(&quot;CurrentDomain_UnhandledException&quot;);
if (e != null)
{
Console.Out.WriteLine(&quot;IsTerminating: '{0}'&quot;, e.IsTerminating);
if (e.ExceptionObject != null)
Console.Out.WriteLine(&quot;ExceptionObject: '{0}'&quot;, e.ExceptionObject.ToString());
}
}
</pre>
<p>The .NET platform had various behaviours regarding how dealing with this event. Please refer to the MSDN documentation for further details, but here are the main points:</p>
<ul>
<li>This event (<em><strong>UnhandledException</strong></em>)<span style="text-decoration:underline;"> provides</span> <strong>notification</strong> of<strong> uncaught exceptions</strong>.</li>
<li>The<strong> system default handler</strong> <span style="text-decoration:underline;">reports</span> the exception to the user (<strong>debuging options</strong>).
<ul>
<li>.NET 1.1 :<strong> debugging options</strong> are reported to the user before this <strong>event</strong> is raised.</li>
<li>Since .NET 2.0: <strong>event</strong> is raised then <strong>debugging options</strong> are reported to the user.</li>
</ul>
</li>
<li>The <strong>system default handler</strong><span style="text-decoration:underline;"> terminates</span>the application.
<ul>
<li>.NET 1.1 : An unhandled exception that occurs in a thread <em><strong>other than the main application thread</strong></em> is caught by the runtime and therefore <em><strong>does not cause the application to terminate</strong></em>. Thus, it is possible for the <strong>UnhandledException event</strong> <span style="text-decoration:underline;">to be raised</span> <strong>without the application terminating</strong>.</li>
<li>Since .NET 2.0: This <strong>backstop</strong> for unhandled exceptions in child threads <strong>was removed</strong>, because the cumulative effect of such<strong> silent failures</strong> included <strong><em>performance degradation</em></strong>, <em><strong>corrupted data</strong></em>, and <em><strong>lockups</strong></em>, all of which <span style="text-decoration:underline;">were difficult to debug.</span></li>
</ul>
</li>
<li>This <strong>event</strong> can be handled in an <strong>application domain</strong>.
<ul>
<li>.NET 1.1 : this <strong>event</strong> occurs only for the <strong>default application domain</strong> that is created by the CLR when an application is started.</li>
<li>Since .NET 2.0 (extract from MSDN <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" target="_blank">[Ref-02]</a> ):</li>
</ul>
</li>
</ul>
<blockquote>
<p style="padding-left:60px;">If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. If the thread started in an application domain that has an event handler for UnhandledException, the event is raised in that application domain. If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains.</p>
<p style="padding-left:60px;">For example, suppose a thread starts in application domain &#8220;AD1&#8243;, calls a method in application domain &#8220;AD2&#8243;, and from there calls a method in application domain &#8220;AD3&#8243;, where it throws an exception. The first application domain in which the UnhandledException event can be raised is &#8220;AD1&#8243;. If that application domain is not the default application domain, the event can also be raised in the default application domain.</p>
</blockquote>
<p>Please, refer to the MSDN documentation, <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" target="_blank">[Ref-02]</a> &amp; <a href="http://msdn.microsoft.com/en-us/library/ms228965.aspx" target="_blank">[Ref-04]</a>, as there are other special cases that you might need to look at more in details depending in your code and usage of threads. E.g. what happens for the <em><strong>&#8216;finalizer thread&#8217;</strong></em> or <em><strong>&#8216;worker thread&#8217;</strong></em> from the .NET thread Pool <em>(I am not putting this information here, otherwise this entry will start deriving on &#8216;multithread programming&#8217; and I want it to stay on &#8216;Exception programming on the .NET platform&#8217;)</em>.</p>
<p>As I mentioned early, there some exceptions for which the CLR will not execute your catch{} or finally{} blocks, e.g. when a &#8220;StackOverflow&#8221; or &#8220;Access violation&#8221; exception is raised. In those cases, the CLR will assume that your process is in a very bad state and will terminate your process. With the <strong>.NET v4.0</strong> the CLR put in place a flag (either in the config file or by using an attribute in code called &#8216;<strong>[HandleProcessCorruptedStateExceptions]</strong>&#8216;) that you can use to indicate you want your handler to be called.</p>
<p>There is also an excellent article about &#8220;Handling Corrupted States Exceptions&#8221; (aka, StackOverflow and Access violation) written by Andrew Pardoe <a href="http://msdn.microsoft.com/en-us/magazine/dd419661.aspx" target="_blank">[Ref #03]</a>.</p>
<h1>4 - The optional &#8216;finally&#8217; block</h1>
<p>You can add a &#8216;finally&#8217; block at the end of your try/catch block(s).</p>
<p>&#8216;<strong>finally</strong>&#8216; is an interesting construct as it allows your code inside it to be executed whatever happened in the try/catch block(s) (except for a few cases, e.g. if a &#8220;StackOverflow&#8221; exception is raised).</p>
<p>However, you should be careful about what you are doing inside the &#8216;finally&#8217; block:<br />
1) Check for &#8216;null&#8217; references before using a object<br />
2) You might need to have another try/catch block to catch any new exception, so you do not loose the original exception.</p>
<h1>Conclusion</h1>
<p>I hope that by now you have a better understanding of try/catch/finally blocks and better knoweldge on the various away to raise/re-raise, add information or ignore the exception.</p>
<p>My last advise will be <em><strong>&#8216;no exception should go completely silent, at least it should be logged for later analysis&#8217;</strong></em>.</p>
<p>You will find the in the sample C# project the various implementation of the cases exposes in this entry blog. Have fun with it.</p>
<h2><span style="text-decoration:underline;">References:</span></h2>
<ul>
<li>[REF-01] MSDN, <a title="Exception Class" href="http://msdn.microsoft.com/en-us/library/system.exception.aspx" target="_blank">Exception Class</a>.</li>
<li>[REF-02] MSDN, <a title="UnhandledException event" href="http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx" target="_blank">AppDomain.UnhandledException Event</a>.</li>
<li>[REF-03] <a title="Handling Corrupted State Exceptions, Feb-2009" href="http://msdn.microsoft.com/en-us/magazine/dd419661.aspx" target="_blank">&#8220;Handling Corrupted State Exceptions&#8221;</a>, Andrew Pardoe, MSDN Magazine, CLR Inside Out (2009-February).</li>
<li>[REF-04] MSDN, <a title="Exceptions in Managed Threads" href="http://msdn.microsoft.com/en-us/library/ms228965.aspx" target="_blank">&#8220;Exceptions in Managed Threads&#8221;</a>.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/494/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/494/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=494&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/01/13/exception-how-to-raise-and-retrieve-error-information-whithout-losing-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/12/exdemo_logo.png" medium="image">
			<media:title type="html">ExDemo_logo</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/12/exdemo_callschain.png" medium="image">
			<media:title type="html">ExDemo_CallsChain</media:title>
		</media:content>
	</item>
		<item>
		<title>PerfView 1.0 &#8211; A new performance analysis tool</title>
		<link>http://blog.ilab8.com/2012/01/04/perfview-1-0-a-new-performance-analysis-tool/</link>
		<comments>http://blog.ilab8.com/2012/01/04/perfview-1-0-a-new-performance-analysis-tool/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 11:14:42 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Troubleshooting]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=535</guid>
		<description><![CDATA[Microsoft has just released a new performance analysis tool, called PerfView (download here). It can help to isolate CPU- and memory-related performance issues. As I do a few performance analysis per month, this is a very good news &#8211; something new to try. it looks like it can handle large memory dump files (a lot of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=535&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2012/01/perfview_smalllogo.png"><img class="alignleft size-full wp-image-537" style="margin:5px;" title="Perfview_smallLogo" src="http://ilab8.files.wordpress.com/2012/01/perfview_smalllogo.png?w=600" alt=""   /></a>Microsoft has just released a new performance analysis tool, called PerfView (<a title="PerfView download page" href="http://www.microsoft.com/download/en/details.aspx?id=28567" target="_blank">download here</a>).</p>
<p>It can help to isolate CPU- and memory-related performance issues. As I do a few performance analysis per month, this is a very good news &#8211; something new to try.<span id="more-535"></span></p>
<p>it looks like it can handle large memory dump files (a lot of Gb). Very good news because now with 64 application processes, dump files are getting bigger (greater than 4Gb of data and code to be analysed) and finding the right information is taking more time than before. Hopefully this tool will help us in that new world &#8230;</p>
<p>Here a few other links on the subject:</p>
<ul>
<li>Blog entry from Vance Morrison,  28th Dec 2011, <a title="Vance Morrison's blog entry on PerfView" href="http://blogs.msdn.com/b/vancem/archive/2011/12/28/publication-of-the-perfview-performance-analysis-tool.aspx" target="_blank">&#8220;Publication of the PerfView performance analysis tool!&#8221;</a>.</li>
<li>Blog entry from DougSte,  4th Jan 2012, <a title="DougSte - blog entry" href="http://blogs.msdn.com/b/dougste/archive/2012/01/04/new-clr-and-net-performance-tool.aspx" target="_blank">&#8220;New CLR and .NET Performance tool&#8221;</a>.</li>
</ul>
<p>Thanks to my colleague <a title="Julien Brunet's web log" href="http://www.topian.net/" target="_blank">Julien Brunet</a> to point out this tool to me this morning.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/535/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/535/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=535&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2012/01/04/perfview-1-0-a-new-performance-analysis-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2012/01/perfview_smalllogo.png" medium="image">
			<media:title type="html">Perfview_smallLogo</media:title>
		</media:content>
	</item>
		<item>
		<title>Windows Phone &#8211; Where are we at the end of 2011?</title>
		<link>http://blog.ilab8.com/2011/12/31/windows-phone-where-are-we-at-the-end-of-2011/</link>
		<comments>http://blog.ilab8.com/2011/12/31/windows-phone-where-are-we-at-the-end-of-2011/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 13:26:57 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=525</guid>
		<description><![CDATA[In 2010, I attended the TechEd Europe in Berlin and Windows Phone was one of the keynotes at the time and the fact that it was launched in Europe in October 2010. Fourteen months later, where are we? 1.5% of global market [ref-01] (Gartner source). 14 months to get up to 50,000 applications available on Windows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=525&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div id="attachment_30" class="wp-caption alignleft" style="width: 237px"><a href="http://ilab8.files.wordpress.com/2010/11/wph-title_small.png"><img class="size-full wp-image-30 " title="WPH-Title_small" src="http://ilab8.files.wordpress.com/2010/11/wph-title_small.png?w=600" alt="Windows Phone"   /></a><p class="wp-caption-text">Windows Phone 7</p></div>
<p>In 2010, I attended the <a title="TechEd 2010 – Day #1" href="http://blog.ilab8.com/2010/11/08/teched-2010-day-2/" target="_blank">TechEd Europe in Berlin</a> and Windows Phone was one of the keynotes at the time and the fact that it was launched in Europe in October 2010. Fourteen months later, where are we?<span id="more-525"></span></p>
<ul>
<li>1.5% of global market [ref-01] (Gartner source).</li>
<li>14 months to get up to 50,000 applications available on Windows Phone 7 Marketplace (To reach that number, apple took 12 months and android 18 months) [Ref-02].</li>
</ul>
<p>An interesting blog entry by Charlie Kindel, <a href="http://ceklog.kindel.com/2011/12/26/windows-phone-is-superior-why-hasnt-it-taken-off/" target="_blank">&#8220;Windows Phone is Superior; Why Hasn’t it Taken Off?&#8221;</a> [Ref-03].</p>
<p>As a owner of Samsung Omnia 7 and user of Windows Phone 7 and now 7.5 (aka Mango) I can say that:</p>
<ul>
<li>I really loved the way &#8220;Windows Phone&#8221; was done as a &#8220;start from a green field and forget about windows mobile !!&#8221;.</li>
<li>I am very happy with my Windows phone, specially with the various Hubs (People, Games, Messaging &#8230;) were your information is aggregated and linked for you, making those &#8216;information spaces&#8217; more useful;</li>
<li>The upgrade, from 7.0 to 7.5 went smoothly and was successful;</li>
<li>I use Zune to sync my mobile with my computer (notes, pictures, movies, podcats&#8230;) and never had any serious issue.</li>
<li>It took sometime to get the applications available, but they are coming.</li>
</ul>
<p>I hope that the partnership between Microsoft and Nokia, and Microsoft and Skype will bring and put Windows Phone in to a better place and position regarding global market place and more end users will be able to have a good and different experience with their phones.</p>
<p>So, this will be my last one for this year. Happy new year 2012 !!</p>
<p><span style="text-decoration:underline;">References</span></p>
<ul>
<li>[Ref-01] Gartner Press Release, 15th Nov 2011,<a title="Gartner Press Releases" href="http://www.gartner.com/it/page.jsp?id=1848514" target="_blank"> &#8220;Gartner Says Sales of Mobile Devices Grew 5.6 Percent in Third Quarter of 2011; Smartphone Sales Increased 42 Percent&#8221;</a>.</li>
<li>[Ref-02] All About Windows Phone, by Rafe Blandford, 27th Dec 2011, <a title="All About Windows Phone" href="http://allaboutwindowsphone.com/news/item/13913_Windows_Phone_Marketplace_pass.php" target="_blank">&#8220;Windows Phone Marketplace passes 50,000 apps&#8221;</a>.</li>
<li>[Ref-03] Charlie Kindel&#8217;s blog entry, 26th Dec 2011, <a title="Charlie Kindel" href="http://ceklog.kindel.com/2011/12/26/windows-phone-is-superior-why-hasnt-it-taken-off/" target="_blank">&#8220;Windows Phone is Superior; Why Hasn’t it Taken Off?&#8221;</a>.</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/525/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=525&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2011/12/31/windows-phone-where-are-we-at-the-end-of-2011/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2010/11/wph-title_small.png" medium="image">
			<media:title type="html">WPH-Title_small</media:title>
		</media:content>
	</item>
		<item>
		<title>On my way to Build Windows conference</title>
		<link>http://blog.ilab8.com/2011/09/07/on-my-way-to-build-windows-conference/</link>
		<comments>http://blog.ilab8.com/2011/09/07/on-my-way-to-build-windows-conference/#comments</comments>
		<pubDate>Wed, 07 Sep 2011 21:40:56 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[Build 2011]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=435</guid>
		<description><![CDATA[I am looking forward to coming to this event in the USA and also to visiting my friends. Earlier this year, TechEd Europe 2011 (usually in November) was cancelled (or “postponed” to June 2012). So, it had to be in USA. Since last March, I was searching the web for the PDC or TechEd conference [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=435&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a title="MS Build 2011" href="http://www.buildwindows.com/" target="_blank"><img class="aligncenter size-full wp-image-436" title="Build Conference small logo" src="http://ilab8.files.wordpress.com/2011/09/buildconfsmallicon.png?w=600" alt="Build 2011 Conference"   /></a></p>
<p>I am looking forward to coming to this event in the USA and also to visiting my friends.</p>
<p>Earlier this year, TechEd Europe 2011 (usually in November) was cancelled (or “postponed” to June 2012). So, it had to be in USA.</p>
<p>Since last March, I was searching the web for the PDC or TechEd conference in USA &#8211; 2012 is going to be a major year in term of releases from Microsoft (a lot of new products, enhancement on .NET platform and development tools and maybe a new OS &#8211; <em>Windows 8</em>). It was a relief when I managed to get a little more information about the Build conference back in May. However, there was not any agenda and even the dates were not settled. Finally, on 13th June I managed to book it. Apparently, MS started to sell tickets on 1st June and they sold out on 1st August (2 months &#8211; no surprise, when you cancel (or &#8220;postpone&#8221;) other events).</p>
<p>Summer is gone in Europe. In a few hours time, I will be on my way to this conference&#8230;</p>
<p>Like last year, at TechEd Europe in Berlin, I will post information/observations about the sessions. See you there !!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/435/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/435/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=435&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2011/09/07/on-my-way-to-build-windows-conference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/09/buildconfsmallicon.png" medium="image">
			<media:title type="html">Build Conference small logo</media:title>
		</media:content>
	</item>
		<item>
		<title>ODP.NET &#8211; &#8220;Pooling&#8221; and &#8220;Connection request timed out&#8221;</title>
		<link>http://blog.ilab8.com/2011/09/02/odp-net-pooling-and-connection-request-timed-out/</link>
		<comments>http://blog.ilab8.com/2011/09/02/odp-net-pooling-and-connection-request-timed-out/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 22:22:21 +0000</pubDate>
		<dc:creator>Joao Morais</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[Connection pools]]></category>
		<category><![CDATA[ODP.NET]]></category>

		<guid isPermaLink="false">http://blog.ilab8.com/?p=375</guid>
		<description><![CDATA[Recently, I had to look at an issue in our code regarding &#8220;Connection request timed out&#8221;. Our Oracle connections are set to use the Pooling service offered by ODP.NET. Oracle documentation as the following description: &#8220;The Max Pool Size attribute of the ConnectionString property sets the maximum number of connections for a connection pool. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=375&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://ilab8.files.wordpress.com/2011/09/oracle-pooling-small.png"><img class="alignleft size-full wp-image-415" style="margin:5px;" title="Oracle - Pooling" src="http://ilab8.files.wordpress.com/2011/09/oracle-pooling-small.png?w=600" alt="Oracle - Pooling - small"   /></a>Recently, I had to look at an issue in our code regarding &#8220;Connection request timed out&#8221;. Our Oracle connections are set to use the Pooling service offered by ODP.NET. Oracle documentation as the following description:</p>
<blockquote><p><em>&#8220;The Max Pool Size attribute of the ConnectionString property sets the maximum number of connections for a connection pool. If a new connection is requested, but no connections are available and the limit for Max Pool Size has been reached, then the connection pooling service waits for the time defined by the Connection Timeout attribute. If the Connection Timeout time has been reached, and there are still no connections available in the pool, the connection pooling service raises an exception indicating that the connection pool request has timed-out.&#8221; [Ref-01, p3-6]</em></p></blockquote>
<p><span id="more-375"></span></p>
<p>This post is about enabling the tools (traces and performance counters) that will help you to monitor and trace Oracle DB pooling activities in your applications. Also, I am providing some information about the way Oracle client connection pooling works from my observations. In the end, this helped me to track down the &#8220;leak&#8221; in my application.</p>
<p>We are using on the client side the Oracle Data Provider for .NET (aka ODP.NET) for Oracle DB 11g and our server is using Oracle DB 10g.</p>
<p>The &#8220;Connection pool performance counters&#8221; is a new feature of  the ODP.NET for Oracle DB 11g.</p>
<h2>Enabling ODP.NET Traces</h2>
<p>The first thing I tried was to enable Oracle ODP.NET traces. However, the way oracle ODP.NET was installed on the machine was by &#8220;copy deployment&#8221;, so the registry entries were not set.</p>
<p>Oracle documentation (see <a title="Oracle 11g WebDoc" href="http://download.oracle.com/docs/html/E10927_01/featConfig.htm#CJABCACG" target="_blank">[Ref-01]</a> for the doc) provides the required information, i.e.:</p>
<ol>
<li>ODP.NET tracing features are controlled by registry values under the entry key &#8220;HKEY_LOCAL_MACHINE\Software\Oracle\ODP.NET\<em>&lt;version&gt;</em>&#8220;.
<ol>
<li>&lt;version&gt; is the version number of your &#8220;Oracle.DataAccess.dll&#8221;.</li>
<li>This DLL should found under &#8220;&lt;ORACLE_HOME&gt;\odp.net\bin\2.x&#8221;
<ol>
<li>&#8220;2.x&#8221; for .NET 2.0 applications (and also 3.0 and 3.5)</li>
<li>&#8220;1.x&#8221; for .NET 1.1 applications</li>
</ol>
</li>
<li>Mine is version &#8220;2.112.2.0&#8243;, so my entry key is &#8220;HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\ODP.NET\2.112.2.0&#8243;.</li>
</ol>
</li>
<li>There are 3 keys that really matters for Tracing (all &#8216;sz&#8217; entries, i.e. string values):
<ol>
<li>&#8220;TraceLevel&#8221;
<ul>
<li>Traces is enabled depending on the value of key.</li>
<li>Here are the possible values:</li>
<li>
<table border="1">
<tbody>
<tr>
<th>Value</th>
<th>Description</th>
</tr>
<tr>
<td>0</td>
<td>None</td>
</tr>
<tr>
<td>1</td>
<td>Entry, exit, and SQL statement information</td>
</tr>
<tr>
<td>2</td>
<td>Connection Pooling statistics</td>
</tr>
<tr>
<td>4</td>
<td>Distributed transactions (enlistment and delistment)</td>
</tr>
<tr>
<td>8</td>
<td>User-mode dump creation upon unmanaged exception</td>
</tr>
<tr>
<td>16</td>
<td>HA Event Information</td>
</tr>
<tr>
<td>32</td>
<td>Load Balancing Information</td>
</tr>
<tr>
<td>64</td>
<td>Self Tuning Information</td>
</tr>
<tr>
<td>127</td>
<td>All the above</td>
</tr>
<tr>
<td>Default:</td>
<td>0</td>
</tr>
</tbody>
</table>
</li>
<li>ODP.NET does bit-wise checking on the value, so you can enable multiple logs by adding their values.</li>
</ul>
</li>
</ol>
</li>
<li>&#8220;TraceFileName&#8221; (string value)
<ul>
<li>A valid path and filename for the log. E.g. &#8220;C:\temp\oracle.txt&#8221;</li>
<li><em><code>TraceFileName</code></em> specifies the filename that is to be used for logging trace information. If <em><code>TraceOption</code></em> is set to <strong><code>0</code></strong>, the name is used as is. However, if <em><code>TraceOption</code></em> is <strong><code>1</code></strong>, the Thread ID is appended to the filename provided.</li>
</ul>
</li>
<li>&#8220;TraceOption&#8221; (string value)
<ul>
<li><code>TraceOption</code> specifies whether to log trace information in single or multiple files for each Thread ID. If a single trace file is specified, the filename specified in <code>TraceFileName</code> is used. If the multiple trace files option is requested, a Thread ID is appended to the filename provided to create a trace file for each thread.</li>
</ul>
</li>
<ol>
<ul>
<li>0 = Single trace file</li>
<li>1 = Multiple trace files</li>
</ul>
</ol>
</ol>
<p>For pooling info, I used &#8220;Traceslevel=2&#8243;. However, this was not enough and too verbose. So I turned to &#8220;Perfomance counters&#8221;.</p>
<h3>32/64-bit &#8211; Specific details</h3>
<p>Be careful regarding whether your application runs as a 32-bit or 64-bit image, as the registry entry key is not the same:</p>
<ul>
<li>In a 32-bit OS, all applications will run as a 32-bit image and the registry key in that case is &#8220;HKEY_LOCAL_MACHINE\Software\Oracle&#8221;.</li>
<li>In a 64-bit OS, applications might run either as a 32-bit image or 64-bit image.
<ul>
<li>If it runs as 32-bit image, then the entry key in the registry is &#8220;HKEY_LOCAL_MACHINE\Software\<strong>WOW6432node</strong>\Oracle&#8221;.</li>
<li>If it runs as 64-bit image, then the entry key in the registry is &#8220;HKEY_LOCAL_MACHINE\Software\Oracle&#8221;.</li>
</ul>
</li>
</ul>
<h2>Enabling ODP.NET Performance Counters</h2>
<p>As for the &#8220;enabling Traces&#8221;, because Oracle was not installed in the machine by a deployment tool, I had to configure the performance manually.</p>
<h3>Registering the Performance counters</h3>
<p>In order to find what performance counters ODP.NET was instantiating, I used Reflector on &#8220;Oracle.DataAccess.dll&#8221; and found these counters:</p>
<table border="1">
<tbody>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td>HardConnectsPerSecond</td>
<td>RateOfCountsPerSecond64</td>
<td>Number of sessions being established with the Oracle Database Server every second</td>
</tr>
<tr>
<td>HardDisconnectsPerSecond</td>
<td>RateOfCountsPerSecond64</td>
<td>Number of sessions being severed with the Oracle Database Server every second</td>
</tr>
<tr>
<td>SoftConnectsPerSecond</td>
<td>RateOfCountsPerSecond64</td>
<td>Number of active connections originating from connection pools every second</td>
</tr>
<tr>
<td>SoftDisconnectsPerSecond</td>
<td>RateOfCountsPerSecond64</td>
<td>Number of active connections going back to the connection pool every second</td>
</tr>
<tr>
<td>NumberOfActiveConnectionPools</td>
<td>NumberOfItems64</td>
<td>Total number of active connection pools</td>
</tr>
<tr>
<td>NumberOfActiveConnections</td>
<td>NumberOfItems64</td>
<td>Total number of connection in use</td>
</tr>
<tr>
<td>NumberOfFreeConnections</td>
<td>NumberOfItems64</td>
<td>Total number of connections available for use in all the connection pools</td>
</tr>
<tr>
<td>NumberOfInactiveConnectionPools</td>
<td>NumberOfItems64</td>
<td>Number of inactive connection pools that may be disposed soon</td>
</tr>
<tr>
<td>NumberOfNonPooledConnections</td>
<td>NumberOfItems64</td>
<td>Number of non-pooled active connections</td>
</tr>
<tr>
<td>NumberOfPooledConnections</td>
<td>NumberOfItems64</td>
<td>Number of pooled active connections</td>
</tr>
<tr>
<td>NumberOfReclaimedConnections</td>
<td>NumberOfItems64</td>
<td>Number of connections which were garbage collected implicitly</td>
</tr>
<tr>
<td>NumberOfStasisConnections</td>
<td>NumberOfItems64</td>
<td>Number of connections that will be soon available in the pool. User has closed these connections but they are currently awaiting certain actions like transaction completion before they can be placed back into the pool as free connections.</td>
</tr>
</tbody>
</table>
<p>Those counters are &#8220;multi-instance&#8221;, aka different applications running at the same time have each their set of counters. Furthermore, ODP.NET creates a set of counters<em> per Application domain</em> for .NET processes (See [Ref-03] for further details about &#8220;single-instance&#8221; vs. &#8220;multi-instance&#8221; of performance counters).</p>
<p>I did a little C# application to create those counters using the class &#8220;<em>System.Diagnostics.PerformanceCounterCategory</em>&#8221; to create (or delete) a caterogy of  performance counter (here, ODP.NET uses &#8220;&#8221; as its category of counters, so I used the same).</p>
<p>Here is a sample of code to create the category of performance counters:</p>
<pre class="brush: csharp; title: ; notranslate">

// Create the collection for the counters

CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();
 // Add counters
 CounterCreationData rateCount64 = new CounterCreationData();
 rateCount64.CounterType = PerformanceCounterType.RateOfCountsPerSecond64;
 rateCount64.CounterName = &quot;HardDisconnectsPerSecond&quot;;
 rateCount64.CounterHelp = &quot;Number of sessions being severed with the Oracle Database Server every second&quot;;
 counterDataCollection.Add(rateCount64);

...

// Create and Register the category.
 string categoryName = OracleCategoryConst;
 string categoryHelp = @&quot;Oracle Data Provider for .NET counters&quot;;

PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.MultiInstance, counterDataCollection);

</pre>
<p>To remove it, you can use this piece of code:</p>
<pre class="brush: csharp; title: ; notranslate">

PerformanceCounterCategory.Delete(categoryName);

</pre>
<h3>Enabling PerfCounters for ODP.NET</h3>
<p>Once the performance counters are created and registered in the performance system, you need to tell ODP.NET to start creating and updating the counters. To achieve that, you need to add another string key in the registry called &#8220;<em>PerformanceCounters</em>&#8221; under &#8220;HKEY_LOCAL_MACHINE\Software\Oracle\ODP.NET\<em>&lt;version&gt;</em>&#8221; (see section &#8220;Enabling ODP.NET Traces&#8221;).</p>
<p>The possible values are:</p>
<table border="1">
<tbody>
<tr>
<th>Value</th>
<th>Description</th>
</tr>
<tr>
<td>0</td>
<td>Not Enabled</td>
</tr>
<tr>
<td>1</td>
<td>Number of sessions being established with Oracle Database every second.</td>
</tr>
<tr>
<td>2</td>
<td>Number of sessions being severed from Oracle Database every second.</td>
</tr>
<tr>
<td>4</td>
<td>Number of active connections originating from connection pools every second.</td>
</tr>
<tr>
<td>8</td>
<td>Number of active connections going back to the connection pool every second.</td>
</tr>
<tr>
<td>16</td>
<td>Total number of active connections.</td>
</tr>
<tr>
<td>32</td>
<td>Number of inactive connection pools.</td>
</tr>
<tr>
<td>64</td>
<td>Total number of connections in use.</td>
</tr>
<tr>
<td>128</td>
<td>Total number of connections available for use in all the connection pools.</td>
</tr>
<tr>
<td>256</td>
<td>Number of pooled active connections.</td>
</tr>
<tr>
<td>1024</td>
<td>Number of non-pooled active connections.</td>
</tr>
<tr>
<td>2048</td>
<td>Number of connections that will be soon available in the pool. User has closed these connections, but they are currently awaiting actions, such transaction completion, before they can be placed back into the pool as free connections.</td>
</tr>
<tr>
<td>4095</td>
<td>All the above</td>
</tr>
<tr>
<td>Default:</td>
<td>0</td>
</tr>
</tbody>
</table>
<h3>Launching PerfMon.exe</h3>
<p>Launch &#8220;Perfmon.exe&#8221; from a command line window. You should be able to see the Oracle ODP.NET performance counters called &#8220;Oracle Data Provider for .NET&#8221; when trying to add them to the monitoring screen:</p>
<p style="text-align:center;"><a href="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-addcounters.png" target="_blank"><img class="aligncenter size-full wp-image-394" title="Oracle-Perfmon-AddCounters" src="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-addcounters.png?w=600" alt=""   /></a></p>
<p><em><strong>Be careful</strong> to not confuse it with &#8220;.NET Provider for Oracle&#8221; which is the category of performance counters registered and exposed by Microsoft .NET Provider. </em></p>
<p>In screenshot below I have added both categories of counters:</p>
<p style="text-align:center;"><a href="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-allcounters.png" target="_blank"><img class="aligncenter size-full wp-image-393" title="Oracle-Perfmon-AllCounters" src="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-allcounters.png?w=600" alt=""   /></a></p>
<ul>
<li>The top one is from Microsoft (in fact from Visual Studio &#8220;Server Explorer&#8221; view that I used to connect to Oracle databases for development).</li>
<li>The bottom one is from ODP.NET (from a test application).</li>
</ul>
<p>The naming convention for the instance of perfomance counters for ODP.NET is as follow:</p>
<blockquote><p>&lt;AppDomain name&gt;[&lt;ProcessID&gt;, &lt;AppDomain Index&gt;]</p></blockquote>
<p>So, we can see that in my test application there are 2 AppDomains, one called &#8220;<span style="color:#0000ff;"><strong>ad #2</strong></span>&#8221; and the other one &#8220;<strong>testoraclepoolapp2.exe</strong>&#8221; both from the process (<span style="color:#0000ff;"><span style="color:#000000;">ProcessID:</span> <strong>5736</strong></span>). Each application pool has it owns set of pools (2 for &#8220;<span style="color:#0000ff;"><strong>ad #2</strong></span>&#8221; and 1 for &#8220;<span style="color:#0000ff;"><strong>testoraclepoolapp2.exe</strong></span>&#8220;) and connections.</p>
<p>* Before launching your application using ODP.NET, you should have the performance counters created and registered as well as enabling them via the registry entry &#8220;&#8221; (see previous sections). If your application was already running, you must restart it (this is because ODP.NET will create its counters when the application (to be more precise the AppDomain) is launched and uses Oracle Db connection the first time).</p>
<h4>32-bit processes on 64-bit OS machines</h4>
<p>On 64-bit OS machines, when you launch &#8220;Perfmon.exe&#8221; it will execute the 64-bit version which monitors 64bit counter applications. If like me you need to launch &#8220;Perfmon.exe&#8221; 32-bit to monitor 32 bit counter applications, then you can do it using this command: &#8220;<strong>mmc /32 perfmon.msc</strong>&#8220;  (thanks to<a title="Launching perfmon 32-bit on 64-bit OS" href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/12/02/missing-perfmon-counters-using-64-bit-windows-mmc-32-perfmon-msc.aspx" target="_blank"> Tony Rogerson&#8217;s post [Ref-02]</a>).</p>
<h2>Connection Pool (by ODP.NET)</h2>
<p>Now that I have got access to traces and performance counters I can monitor my application more closely and see what is going on in the world of Oracle Db connections within my application.</p>
<p>However, to be able to read the information about ODP.NET performance counters you need first to understand how &#8220;connection pooling&#8221; works.</p>
<blockquote><p><a title="Definition" href="http://en.wikipedia.org/wiki/Connection_pool" target="_blank"><em>From Wikipedia</em></a></p>
<p><em>In <a title="Software engineering" href="http://en.wikipedia.org/wiki/Software_engineering">software engineering</a>, a <strong>connection pool</strong> is a <a title="Cache" href="http://en.wikipedia.org/wiki/Cache">cache</a> of <a title="Database connection" href="http://en.wikipedia.org/wiki/Database_connection">database connections</a> maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven <a title="Website" href="http://en.wikipedia.org/wiki/Website">website</a> application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used over again so that a new connection does not have to be established. If all the connections are being used, a new connection is made and is added to the pool. Connection pooling also cuts down on the amount of time a user must wait to establish a connection to the database.</em></p></blockquote>
<h3>The key &#8211; Connection string</h3>
<p>How does oracle identifies you need a connection from the pool or not?</p>
<p>It is from the connection string. The connection for Oracle ODP.NET has about 22 parameters that can be set. Usually, you will pass at least 3 of them: &#8220;<strong>Data Source</strong>&#8220;, &#8220;<strong>User ID</strong>&#8221; and &#8220;<strong>Password</strong>&#8220;. All the others will be set with their default values (see [Ref-01]). The parameter that switch ON or OFF the pooling service for the the connection is named &#8220;<strong>Pooling</strong>&#8220;. By default, it is set to &#8216;<em>True</em>&#8216;. So , if do not specify a value of &#8216;<em>False</em>&#8216;, then your connections are coming from a pool.</p>
<p>As long as you used the same connection string (order does not matter, as ODP.NET internally will parse your connection string to rebuild a ordered one), then the Pooling service of ODP.NET can retrieve a connection from the pool.</p>
<p>There 7 parameters in the connection string that control the behavior of the Pooling service: &#8220;<strong>Connection Lifetime</strong>&#8220;, &#8220;<strong>Connection Timeout</strong>&#8220;, &#8220;<strong>Decr Pool Size</strong>&#8220;, &#8220;<strong>HA Events</strong>&#8220;, &#8220;<strong>Incr Pool Size</strong>&#8220;, &#8220;<strong>Load Balancing</strong>&#8220;, &#8220;<strong>Max Pool Size</strong>&#8220;, &#8220;<strong>Min Pool Size</strong>&#8220;, &#8220;<strong>Pooling</strong>&#8220;, &#8220;<strong>Validate Connection</strong>&#8220;. I am going to explained some of them but not all (for &#8220;HA Events&#8221; and &#8220;Load Balancing&#8221; see [Ref-01, p3-5] for further details]).</p>
<h3>Life in the Pool and outside it</h3>
<p>When you ask ODP.NET to open a connection [&lt;<em>OracleConnection&gt;.Open()</em>] and &#8220;Pooling&#8221; is enabled, then the pooling service will search for a pool that matches your connection string:</p>
<ul>
<li>If none exists (i.e. first time this connection was used in your application), then it will create a pool and initialize it with the number of &lt;<em>OracleConnection</em>&gt; defined by &#8220;<strong>Min Pool Size</strong>&#8221; (<em>default: 1</em>). This number is the lower limit of the pool, i.e. there will always be at least &#8220;<strong>Min Pool Size</strong>&#8221; connections in the pool. Only when the pool gets discarded/released the number of connection will go below this value.</li>
<li> If the pool already exists, it will check whether there is a connection available.
<ul>
<li>If there is one available, <span style="color:#0000ff;">the connection is taken out of the pool and pass it back to the application.</span>
<ul>
<li>If &#8220;<strong>Validate Connection</strong>&#8221; is enabled, then a round-trip to the database will be done to <em>&#8216;validate&#8217;</em> the connection.</li>
</ul>
</li>
<li>Else (<em>there is none available</em>) it will check whether the pool has reached its upper limit of connections defined by &#8220;<strong>Max Pool Size</strong>&#8221; (<em>default: 100</em>).
<ul>
<li>If ( NbConnectionsInPool == &#8220;<strong>Max Pool Size</strong>&#8220;), then it will wait for a period of time defined by &#8220;Connection Timeout&#8221; to see whether a connection gets back into the pool, so it can be reused.
<ul>
<li>If (at least) one connection got back into the pool,<span style="color:#0000ff;"> then  the connection is taken out of the pool and pass it back to the application</span>.</li>
<li>Otherwise, it will raise the (in)famous &#8221;<span style="color:#ff0000;">Connection request timed out</span>&#8221; exception (hence this blog entry).</li>
</ul>
</li>
<li>Else it will create new instance(s) of &lt;<em>OracleConnection</em>&gt; defined by &#8220;<strong>Incr Pool Size</strong>&#8221; <em>(default: 5)</em>. <span style="color:#0000ff;"> One of the created connection is taken out of the pool and passed back to the application</span>.</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Once your application has finished to use the &lt;<em>OracleConnection</em>&gt;, then it must call Close()/Dispose(). The pooling service checks whether the <strong>lifetime</strong> of connection has exceeded the value defined by &#8220;<strong>Connection Lifetime</strong>&#8221; (<em>default: 0 &#8211; i.e. no checks</em>).:</p>
<ul>
<li>If the <strong>lifetime</strong> of the connection exceeds this property value, <span style="color:#ff6600;">the connection is closed &#8211; i.e. the physical network connection between your process and the machine hosting the database is closed</span>.</li>
<li>The lifetime of a connection is only checked and enforced by the polling service when a connection is put back into the pool.</li>
<li>However, Connections that have exceeded their lifetimes <span style="color:#0000ff;">are not closed </span> if doing so brings the number of connections in the pool below the &#8220;<strong>Min Pool Size</strong>&#8220;.</li>
</ul>
<p>The connection pooling service closes connections when they are not used &#8211; connections are closed every 3 minutes. The pooling service is allowed to close connection instances every 3 minutes up to the limit defined by the parameter &#8220;<strong>Decr Pool Size</strong>&#8221; (default: 1) of the connection string.</p>
<p>Also you have to remember that ODP.NET is for .NET applications. In .NET, the excution boundary is defined at the <strong>Application Domain </strong>level (aka <strong>AppDomain</strong>). ODP.NET manages the pools per AppDomain. This means that an application using a <span style="color:#0000ff;">connection string &#8220;A&#8221;</span> used in<strong> <span style="color:#008000;">AppDomain #1</span></strong> and the same connection string used in <strong><span style="color:#ff6600;">AppDomain#2</span></strong> (within the same process) will end up with<strong><span style="color:#008000;"> one pool created in AppDomain#1</span></strong> and<strong><span style="color:#ff6600;"> another in AppDomain #2</span></strong>.</p>
<h3>Putting the information in Picture</h3>
<p>Here is a sample picture that show you the relation ship between <strong>ODP.NET performance counters</strong> and the<strong> various connection objects</strong> in a fictive .NET:</p>
<p style="text-align:center;"><a href="http://ilab8.files.wordpress.com/2011/08/oracle-odp-net-connectionpoolspicture.png" target="_blank"><img class="aligncenter size-full wp-image-402" title="ODP.NET - Overview of connection pools" src="http://ilab8.files.wordpress.com/2011/08/oracle-odp-net-connectionpoolspicture.png?w=600" alt="Connection pools and Performance counters"   /></a>I hope this information can help you. As for me, we identified the issue in our application: leak overtime of our connection objects &#8211; i.e. not being disposed/closed properly.</p>
<p>[Ref-01] <a title="Oracle 11g WebDoc" href="http://download.oracle.com/docs/html/E10927_01/featConfig.htm#CJABCACG" target="_blank">Oracle ODP.NET, web documentation</a></p>
<p>[Ref-02] <a title="Launching perfmon 32-bit on 64-bit OS" href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/12/02/missing-perfmon-counters-using-64-bit-windows-mmc-32-perfmon-msc.aspx" target="_blank">&#8220;Missing PERFMON counters using 64 bit Windows -&gt; MMC /32 PERFMON.MSC&#8221;, Tony Rogerson&#8217;s blog</a></p>
<p>[Ref-03]<a title="Single instance, multi-instance and WMI" href="http://blogs.msdn.com/b/bclteam/archive/2004/10/29/249799.aspx" target="_blank"> BCL team Blog, PerformanceCounters: single instance, multi-instance and WMI [David Gutierrez]</a></p>
<p>[Ref-04] <a title="Connection String Attributes" href="http://download.oracle.com/docs/html/E10927_01/featConnecting.htm#i1006259" target="_blank">Connecting to Oracle Database, web documentation</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ilab8.wordpress.com/375/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ilab8.wordpress.com/375/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.ilab8.com&#038;blog=17670602&#038;post=375&#038;subd=ilab8&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.ilab8.com/2011/09/02/odp-net-pooling-and-connection-request-timed-out/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/746a0c18eab9b1f1d6a6d29294760e3e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ilab8</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/09/oracle-pooling-small.png" medium="image">
			<media:title type="html">Oracle - Pooling</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-addcounters.png" medium="image">
			<media:title type="html">Oracle-Perfmon-AddCounters</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/08/oracle-perfmon-allcounters.png" medium="image">
			<media:title type="html">Oracle-Perfmon-AllCounters</media:title>
		</media:content>

		<media:content url="http://ilab8.files.wordpress.com/2011/08/oracle-odp-net-connectionpoolspicture.png" medium="image">
			<media:title type="html">ODP.NET - Overview of connection pools</media:title>
		</media:content>
	</item>
	</channel>
</rss>
