<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnet6.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joris Dresselaers</title><link>http://www.dotnet6.com/blogs/jokke/default.aspx</link><description>I want to know God&amp;#39;s thoughts... the rest are details. - Albert Einstein</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Build: 20611.960)</generator><item><title>Keeping track of your personal projects</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/06/24/keeping-track-of-your-personal-projects.aspx</link><pubDate>Tue, 24 Jun 2008 14:30:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1493</guid><dc:creator>Jokke</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1493</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/06/24/keeping-track-of-your-personal-projects.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m sure a lot of you know the drag of keeping track of personal ToDo-lists. You write down some stuff on a piece of paper and the next day, this paper has mysteriously vanished. Or you save them in an excel sheet, which is probably only available on your home PC. As I currently have a lot going on, I began a search for an online system that could fulfill my needs. Nothing fancy, just some basic functionalities. In my search, I landed on &lt;a href="http://www.tadalist.com/" target="_blank"&gt;Tada List&lt;/a&gt;. This is a very basic online ToDo-list system. &lt;a href="http://www.techcrunch.com/2006/05/08/do-more-online-to-do-lists-compared/" target="_blank"&gt;This article&lt;/a&gt; compares a few online systems. The Tada list comes in first and I can see why. It&amp;#39;s a good tip to keep your personal life a little more organized.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Greetings. &lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1493" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Organization/default.aspx">Organization</category></item><item><title>Assemble your own Ajax progress image</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/05/14/assemble-your-own-ajax-progress-image.aspx</link><pubDate>Wed, 14 May 2008 09:33:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1463</guid><dc:creator>Jokke</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1463</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/05/14/assemble-your-own-ajax-progress-image.aspx#comments</comments><description>&lt;p&gt;Hi,&lt;/p&gt;&lt;p&gt;I was looking for a nice loading image to show the progress of my page when making an Ajax call.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I found &lt;a href="http://www.ajaxload.info/" target="_blank"&gt;a nice website&lt;/a&gt; on which we can assemble our own progress images to use in combination with the UpdateProgress Ajax control.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;An example below:&lt;br /&gt;&lt;img src="http://www.dotnet6.com/blogs/jokke/ajax-loader.gif" height="66" width="66" alt="" /&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Regards,&lt;br /&gt;Joris.&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1463" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Ajax/default.aspx">Ajax</category><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>BMW M3 with M-DCT Available!</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/04/02/bmw-m3-with-m-dct-available.aspx</link><pubDate>Wed, 02 Apr 2008 12:59:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1412</guid><dc:creator>Jokke</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1412</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/04/02/bmw-m3-with-m-dct-available.aspx#comments</comments><description>&lt;p&gt;Hi People.&lt;/p&gt;&lt;p&gt;I wanted to share this with you guys, as it is quite cool.&lt;/p&gt;&lt;p&gt;The M-DCT system is a next generation transmission system, following the SMG system from the M5/M6. It is a double clutch transmission to make transitions between gears (7 of them) smoother and to reduce the 0-100 km/h time.&lt;/p&gt;&lt;p&gt;Below are pictures of this new transmission system.&lt;/p&gt;&lt;p&gt;&lt;img src="http://www.classicdriver.com/upload/images/_uk/13652/img02.jpg" title="M3 M-DCT" alt="M3 M-DCT" height="300" width="540" /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;img src="http://i148.photobucket.com/albums/s19/bouxiedidi/DSC07198.jpg?t=1207079062" height="768" width="909" alt="" /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Not everyone was always as positive when talking about the SMG transmission. As the first M3&amp;#39;s with M-DCT are shipping out, we can read the early reviews by the owners. &lt;a href="http://www.m3post.com/forums/showthread.php?t=130007" target="_blank"&gt;Here is an example&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;And for those of you who can&amp;#39;t get enough of this amazing car. &lt;a href="http://www.m3post.com/forums/showthread.php?t=100058" target="_blank"&gt;Here&lt;/a&gt; is another link with pictures of just about every available configuration.&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;It&amp;#39;ll be a few more years until I place my order ;-) &lt;br /&gt;Cheers.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[One more picture to stop]&lt;br /&gt;&lt;img src="http://spots.autogespot.com/files/18_03_2008/c214748364818032008203624_2.jpg" height="600" width="800" alt="" /&gt;&lt;br /&gt;&lt;/p&gt;&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1412" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/BMW/default.aspx">BMW</category></item><item><title>Exclude unwanted code from code coverage</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/03/28/remove-unwanted-code-from-code-coverage.aspx</link><pubDate>Fri, 28 Mar 2008 19:35:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1370</guid><dc:creator>Jokke</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1370</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/03/28/remove-unwanted-code-from-code-coverage.aspx#comments</comments><description>&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;This is the solution for a problem I encountered a while ago. &lt;/p&gt;

&lt;p&gt;When writing tests with Visual Studio, we frequently want to use the Code Coverage tool to evaluate our created tests. Though this is a good tool, we don&amp;#39;t always want all of our code to be included in the coverage result. Let me give you an example.&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;I have created 2 projects:&lt;br /&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A class library [MyProject] with 2 classes: &lt;b&gt;ClassToBeTested &lt;/b&gt;and &lt;b&gt;MyException&lt;/b&gt;&lt;br /&gt;&lt;/li&gt;

&lt;li&gt;A unit test project [MyTestProject] for which I kept the default class. I renamed it to &lt;b&gt;MyProjectUnitTest&lt;/b&gt;&lt;/li&gt;
&lt;b&gt;&lt;br /&gt;&lt;a href="http://www.dotnet6.com/blogs/jokke/CC0.JPG"&gt;&lt;img src="http://www.dotnet6.com/blogs/jokke/CC0.JPG" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/b&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;br /&gt;The classes from the &lt;b&gt;class library&lt;/b&gt; look like this: &lt;br /&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ClassToBeTested&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; MaxParameterLength = 5;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; MethodToBeTested(&lt;span class="kwrd"&gt;string&lt;/span&gt; someParameter)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="rem"&gt;// Validate the parameter&lt;/span&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (someParameter.Length &amp;gt; MaxParameterLength)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; MyException(&lt;span class="str"&gt;&amp;quot;The length of the parameter was too large.&amp;quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Random().Next();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;[System.Serializable]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyException : Exception&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyException() { }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyException(&lt;span class="kwrd"&gt;string&lt;/span&gt; message) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(message) { }&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyException(&lt;span class="kwrd"&gt;string&lt;/span&gt; message, Exception inner) : &lt;span class="kwrd"&gt;base&lt;/span&gt;(message, inner) { }&lt;br /&gt;    &lt;span class="kwrd"&gt;protected&lt;/span&gt; MyException(&lt;br /&gt;      System.Runtime.Serialization.SerializationInfo info,&lt;br /&gt;      System.Runtime.Serialization.StreamingContext context)&lt;br /&gt;        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(info, context) { }&lt;br /&gt;}&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;br /&gt;The &lt;b&gt;Unit Test Class &lt;/b&gt;looks like this (I did snip some generated code):&amp;nbsp;&lt;br /&gt;&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;[TestClass]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyProjectUnitTest&lt;br /&gt;{&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;    [TestMethod]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ClassToBeTestedTest()&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; expected = 0;&lt;br /&gt;        ClassToBeTested classToBeTested = &lt;span class="kwrd"&gt;new&lt;/span&gt; ClassToBeTested();&lt;br /&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; actual = classToBeTested.MethodToBeTested(&lt;span class="str"&gt;&amp;quot;abc&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;        Assert.IsTrue(actual &amp;gt; expected);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [TestMethod]&lt;br /&gt;    [ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(MyException))]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ClassToBeTestedExceptionTest()&lt;br /&gt;    {&lt;br /&gt;        ClassToBeTested classToBeTested = &lt;span class="kwrd"&gt;new&lt;/span&gt; ClassToBeTested();&lt;br /&gt;        &lt;span class="kwrd"&gt;int&lt;/span&gt; actual = classToBeTested.MethodToBeTested(&lt;span class="str"&gt;&amp;quot;abcdef&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;When we run the unit tests with code coverage used for MyProject, we get a &lt;b&gt;code coverage result of &amp;nbsp;62,50%&lt;/b&gt; which is not as much as we would expect in such a small project.&lt;br /&gt;Below, we can see that the reason for this low score, is the Exception class we use.&lt;br /&gt;&lt;a href="http://www.dotnet6.com/blogs/jokke/CC1.JPG"&gt;&lt;img src="http://www.dotnet6.com/blogs/jokke/CC1.JPG" border="0" alt="" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Now, if we create a project with many Exception classes, we do not want to write tests for all of these classes. I want to &lt;b&gt;exclude &lt;/b&gt;this class &lt;b&gt;from code coverage&lt;/b&gt;. If we search the net, we will find that there is no specific solution for this problem. There is a workaround though.&lt;br /&gt;This workaround comes in the form of attributes. We have 2 possibilities:&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;1. [System.Diagnostics.&lt;span&gt;DebuggerNonUserCode&lt;/span&gt;()]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:10pt;font-family:&amp;#39;Courier New&amp;#39;;"&gt;2. [System.Diagnostics.&lt;span&gt;DebuggerHidden&lt;/span&gt;()]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;
The first attribute we can use for both classes and class members. The second one, we can only use for class members: constructors, methods, properties and indexers.&lt;/p&gt;

&lt;p&gt;As we want to exclude the whole class from code coverage, I will opt for attribute 1. We just place it on the MyException class (or any class you want to exclude).&lt;br /&gt;We run the tests again, and take a look at the result:&lt;br /&gt;&lt;a href="http://www.dotnet6.com/blogs/jokke/CC2.JPG"&gt;&lt;img src="http://www.dotnet6.com/blogs/jokke/CC2.JPG" border="0" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only is our &lt;b&gt;code coverage raised to 100%&lt;/b&gt;, the class on which I put the attribute has also disappeared from the results.&lt;br /&gt;&lt;br /&gt; &lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;b&gt;Important note&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;As you may have guessed, there is a drawback to using these attributes. &lt;br /&gt;If you use the &lt;b&gt;DebuggerNonUserCode&lt;/b&gt; or &lt;b&gt;DebuggerHidden&lt;/b&gt; attribute, the code will be ignored completely; you won&amp;#39;t be able to step into the code anymore. Also, any breakpoints you will add, will be ignored completely.&lt;/p&gt;

&lt;p&gt;Make sure you don&amp;#39;t just start adding the attributes for the sake of raising your code coverage score. Use this &lt;b&gt;approach carefully &lt;/b&gt;as you might be excluding code from your results that needs to be tested!&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Good luck.&lt;/p&gt;
&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1370" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Testing/default.aspx">Testing</category><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Thread-safe caching mechanism using a Hashtable</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/02/15/hashtable-concurrency-issues.aspx</link><pubDate>Fri, 15 Feb 2008 16:26:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1175</guid><dc:creator>Jokke</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1175</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/02/15/hashtable-concurrency-issues.aspx#comments</comments><description>&lt;p&gt;Hi. I wanted to start my first (real) blog post with some interesting material ;-)&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;On our current project we have been struggling with the following problem. We need a custom caching system that uses a static Hashtable to cache its items. As we work in a multi-threaded environment, this solutions needs to be thread safe. Which was off-course the difficulty.&lt;br /&gt; &lt;/p&gt;

&lt;p&gt;Down the road we bumped into a few possibilities:&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3"&gt;&lt;u&gt;1. Using the &lt;span style="font-weight:bold;"&gt;lock()&lt;/span&gt; mechanism Visual studio provides&lt;/u&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;This was not a good solution as the creation of the instance was happening within the lock() statement. This had a really bad influence on performance. All threads would be in a waiting state whenever a new instance was created. So we moved on.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;font size="3"&gt;&lt;u&gt;2. Using the &lt;span style="font-weight:bold;"&gt;ReaderWriterLockSlim&lt;/span&gt; class&lt;/u&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;This class is new in the .NET Framework 3.5 and is an improvement on the ReaderWriterLock class in previous versions of the framework. &lt;a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,c4ea3d6d-190a-48f8-a677-44a438d8386b.aspx" target="_blank"&gt;This young man&lt;/a&gt; wrote quite a good blog post on this topic. And &lt;a href="http://devplanet.com/blogs/brianr/archive/2008/01/18/thread-safe-dictionary-in-net.aspx" target="_blank"&gt;this guy&lt;/a&gt; also came up with a thread safe Dictionary implementation (easily convertible into a Hashtable).&lt;/p&gt;
&lt;p&gt;This solution was still no good as we were in need for a finer grained locking mechanism. This means that a lock can only occur when 2 threads are asking for the same object. When ThreadA is waiting on object X to be created, and ThreadB is also asking for object X and ThreadC asks for an instance of object Y, there may not be a waiting period for ThreadC. ThreadB though will always have to wait until ThreadA has completed its work and will then get a reference to the same instance created by ThreadA.&lt;/p&gt;
&lt;p&gt;Close, but no cigar. We moved on again.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;font size="3"&gt;&lt;u&gt;3. The &lt;span style="font-weight:bold;"&gt;solution&lt;/span&gt;&lt;/u&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;After brainstorming with a few colleagues, we came up with a solution.&lt;/p&gt;
&lt;p&gt;Instead of maintaining one Hashtable with the instances, we will maintain 2 Hashtables: one for the instances and one for the storage of locking objects. This way, we would only have to lock the instanceLocks Hashtable whenever we would instantiate a new object. The instance Hashtable would remain untouched during the creation period. The Instance Hashtable would still get locked afterward, but only to add the newly created instances to its collection. You might also recognize the double-check locking pattern to make sure the object being locked didn&amp;#39;t change while locking it.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt; Voila, we found what we were looking for. Below is the code to accomplish this.&lt;/p&gt;
&lt;p&gt;By the way: if you are wondering why the delegate is declared in the namespace; we did this so we can pass the method - which we call to actually create the object - to the &lt;b&gt;Get&lt;/b&gt; method. This way, the creation of the objects is separated from the caching mechanism. &lt;/p&gt;
&lt;p&gt;Hope this is of relevant use to you. Enjoy.&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CustomCaching&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; TContract AnonymousDelegate&amp;lt;TContract&amp;gt;();&lt;br /&gt;&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HashtableCache&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Hashtable instances = &lt;span class="kwrd"&gt;new&lt;/span&gt; Hashtable();&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Hashtable instanceLocks = &lt;span class="kwrd"&gt;new&lt;/span&gt; Hashtable();&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; TContract Get&amp;lt;TContract&amp;gt;(AnonymousDelegate&amp;lt;TContract&amp;gt; anonymousBuildUpDelegate)&lt;br /&gt;            &lt;span class="kwrd"&gt;where&lt;/span&gt; TContract : &lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TContract).FullName;&lt;br /&gt;            TContract contract = &lt;span class="kwrd"&gt;default&lt;/span&gt;(TContract);&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// See if the instance already exists, without taking a lock on it.&lt;/span&gt;&lt;br /&gt;            contract = TryGetInstance&amp;lt;TContract&amp;gt;(key);&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (contract != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; contract;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; instanceLock;&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// The instance wasn&amp;#39;t found on the first try. Now we take a lock on the hashtable.&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (instances)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="rem"&gt;// Some other thread might have accessed the hashtable already, &lt;br /&gt;		// so do a second check.&lt;/span&gt;&lt;br /&gt;                contract = TryGetInstance&amp;lt;TContract&amp;gt;(key);&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (contract != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; contract;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// We still haven&amp;#39;t found the instance, so let&amp;#39;s create it.&lt;/span&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// We keep a dictionary of creation locks per key; &lt;/span&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// this is our fine grained locking mechanism.&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;lock&lt;/span&gt; (instanceLocks)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="rem"&gt;// If the lock is already created by another thread, use it. If not&lt;/span&gt;&lt;br /&gt;                    &lt;span class="rem"&gt;// create one ourselves and add it to the locks hashtable.&lt;/span&gt;&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (instanceLocks.ContainsKey(key))&lt;br /&gt;                    {&lt;br /&gt;                        instanceLock = instanceLocks[key];&lt;br /&gt;                    }&lt;br /&gt;                    &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;                    {&lt;br /&gt;                        instanceLock = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;();&lt;br /&gt;                        instanceLocks.Add(key, instanceLock);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            &lt;span class="rem"&gt;// Now we take a lock on the finer grained object.&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (instanceLock)&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="rem"&gt;// Check again if the singleton was already created. If so return it,&lt;/span&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// after removing the creation lock.&lt;/span&gt;&lt;br /&gt;                contract = TryGetInstance&amp;lt;TContract&amp;gt;(key);&lt;br /&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (contract != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;br /&gt;                {&lt;br /&gt;                    RemoveInstanceLock(key);&lt;br /&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; contract;&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// We can now call the provided delegate to create our contract for us.&lt;/span&gt;&lt;br /&gt;                contract = anonymousBuildUpDelegate();&lt;br /&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// Take a lock on the instances hashtable and add the created instance.&lt;/span&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;lock&lt;/span&gt; (instances)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="rem"&gt;// Double check: if the key does exist, &lt;br /&gt;		    // something has gone wrong.&lt;/span&gt;&lt;br /&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (instances.ContainsKey(key))&lt;br /&gt;                    {&lt;br /&gt;                        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ApplicationException(&lt;span class="kwrd"&gt;&lt;br /&gt;			string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;A duplicate key {0} exists in the hashtable.&amp;quot;&lt;/span&gt;, &lt;br /&gt;				key));&lt;br /&gt;                    }&lt;br /&gt;                    instances.Add(key, contract);&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                &lt;span class="rem"&gt;// We remove the lock object from the locks hashtable&lt;/span&gt;&lt;br /&gt;                RemoveInstanceLock(key);&lt;br /&gt;&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; contract;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RemoveInstanceLock(&lt;span class="kwrd"&gt;string&lt;/span&gt; key)&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="rem"&gt;// Remove the locking object from the dictionary if it exists.&lt;/span&gt;&lt;br /&gt;            &lt;span class="kwrd"&gt;lock&lt;/span&gt; (instanceLocks)&lt;br /&gt;            {&lt;br /&gt;                instanceLocks.Remove(key);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T TryGetInstance&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key) &lt;span class="kwrd"&gt;where&lt;/span&gt; T : &lt;span class="kwrd"&gt;class&lt;/span&gt;&lt;br /&gt;        {&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (instances.ContainsKey(key))&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (T)instances[key];&lt;br /&gt;            }&lt;br /&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1175" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Threading/default.aspx">Threading</category></item><item><title>Blog code formatting</title><link>http://www.dotnet6.com/blogs/jokke/archive/2008/02/14/blog-code-formatting.aspx</link><pubDate>Thu, 14 Feb 2008 13:37:00 GMT</pubDate><guid isPermaLink="false">e5d4c6e2-da0c-4b9e-b9f2-d96787ec4ab1:1178</guid><dc:creator>Jokke</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnet6.com/blogs/jokke/rsscomments.aspx?PostID=1178</wfw:commentRss><comments>http://www.dotnet6.com/blogs/jokke/archive/2008/02/14/blog-code-formatting.aspx#comments</comments><description>&lt;p&gt;Hi guys,&lt;/p&gt;
&lt;p&gt;While writing my first blog post, I stumbled on&amp;nbsp;the following problem: how do I format the code that I want to publish on the blog? It seems that when I paste my code in the blog editor, I lose all formatting (except for the coloring).&lt;/p&gt;
&lt;p&gt;My good friend &lt;a href="http://www.dotnet6.com/blogs/wesley_walraeve/default.aspx" target="_blank"&gt;Wesley&lt;/a&gt; told me that a website exists for creating formatted C# code. In my google search (the first link I came across actually)&amp;nbsp;I found &lt;a href="http://manoli.net/csharpformat/" target="_blank"&gt;a website&lt;/a&gt; that will do exactly that.&lt;/p&gt;
&lt;p&gt;This site can transform C#, vb and some other code types into the correct format. Jaj!&lt;/p&gt;
&lt;p&gt;I will give a before-after example of a testclass I created. Please don&amp;#39;t mind the code, just mind the formatting.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;&lt;font size="4"&gt;Before:&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#000000"&gt; System;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#000000"&gt; System.Collections.Generic;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#000000"&gt; System.Linq;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;
&lt;/font&gt;&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;using&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#000000"&gt; System.Text;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;namespace&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#000000"&gt; Reflection&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;/font&gt;&lt;p&gt;&lt;font size="2"&gt;{&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;static&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;class&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;Class1&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;
&lt;p&gt;{&lt;/p&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;public&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;static&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;void&lt;/font&gt;&lt;font size="2"&gt; SomeMethod(&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;params&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt;[] stackParams)&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;{&lt;/p&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;Class2&lt;/font&gt;&lt;font size="2"&gt;.SomeOtherMethod();&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;/font&gt;
&lt;p&gt;&amp;nbsp;&lt;b&gt;&lt;font size="4"&gt;After:&lt;/font&gt;&lt;/b&gt;&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Reflection&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Class1&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SomeMethod(&lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] stackParams)&lt;br /&gt;        {&lt;br /&gt;            Class2.SomeOtherMethod();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best formatting wishes to you ;-)&lt;/p&gt;&lt;img src="http://www.dotnet6.com/aggbug.aspx?PostID=1178" width="1" height="1"&gt;</description><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/Blogging/default.aspx">Blogging</category><category domain="http://www.dotnet6.com/blogs/jokke/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>