<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Programming</title>
        <link>http://jeffdeville.com/blog/category/1.aspx</link>
        <description>Programming</description>
        <language>en-US</language>
        <copyright>Jeff Deville</copyright>
        <managingEditor>jeffdeville+blog@gmail.com</managingEditor>
        <generator>Subtext Version 1.9.4.0</generator>
        <item>
            <title>Typed Enum Parsing with Generics</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/15/20.aspx</link>
            <description>&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; T Parse&amp;lt;T&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; text)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T).IsEnum)
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T) + &lt;span class="str"&gt;" is not an Enum"&lt;/span&gt;);
    &lt;span class="kwrd"&gt;try&lt;/span&gt;
    {
        return (T)Enum.Parse(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T), text, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; (Exception)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;default&lt;/span&gt;(T);
    }
}
&lt;/pre&gt;
&lt;p&gt;Not much to show here.  I was tired of having to manage all the casting when using the Enum.Parse function.  Unfortunately, I was unable to use this check &lt;/p&gt;
&lt;p&gt;where T : System.Enum&lt;/p&gt;
&lt;p&gt;which would have given a compilation error if you tried to use it to convert a string to an object that wasn't an Enum.  Unfortunately, you can't use that as a Type parameter constraint.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/20.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/15/20.aspx</guid>
            <pubDate>Sun, 15 Apr 2007 04:21:02 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/20.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/15/20.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/20.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/20.aspx</trackback:ping>
        </item>
        <item>
            <title>Threading: Using Delegates - Part 2</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/15/19.aspx</link>
            <description>&lt;p&gt;In &lt;a href="http://jeffdeville.com/blog/archive/2007/04/14/17.aspx"&gt;Part 1&lt;/a&gt;, we began to examine delegates as they applied to multi-threading scenarios.  However, the specific implemenation was not necessarily an optimal one.  The strategy implemented was to periodically check to see if the work was completed or not.  Once it was, EndInvoke was called, and execution continued.  This was a &lt;strong&gt;"Daddy, are we there yet?"&lt;/strong&gt; strategy.  It's as annoying in your code as it is in the car.  There's a better way.  In fact, there are 2.  &lt;/p&gt; &lt;p&gt;The first is very simple.  EndInvoke will block the calling thread until the work is complete.  Let's take another physical world example.  Let's assume it's coming up on lunch time, and you and a coworker are going to &lt;a href="http://www.chipotle.com" target="_blank"&gt;Chipotle&lt;/a&gt; (because there's just no other place you'd ever want to go.)  Unfortunately, he's in a meeting that's running long, and you have to get some code finished before you leave.  How would this look if it was delegate / asynchronous code?&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; TimeForLunch
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WaitForMeetingToEndHandler();

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        WaitForMeetingToEndHandler waitForMeeting = 
            &lt;span class="kwrd"&gt;new&lt;/span&gt; WaitForMeetingToEndHandler(WaitForMeetingToEnd);

        &lt;span class="rem"&gt;// Start waiting on your coworker&lt;/span&gt;
        IAsyncResult result = waitForMeeting.BeginInvoke(&lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;);

        &lt;span class="rem"&gt;// Keep working on your project&lt;/span&gt;
        FinishCode();

        &lt;span class="rem"&gt;// Check to see if your coworker's meeting is out.  If not, wait until it is.&lt;/span&gt;
        waitForMeeting.EndInvoke(result);

        &lt;span class="rem"&gt;// Both tasks are complete.  It's burrito-time.&lt;/span&gt;
        Console.WriteLine(&lt;span class="str"&gt;"Go to lunch."&lt;/span&gt;);

        Console.ReadLine();
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WaitForMeetingToEnd()
    {
        Thread.Sleep(10000);
        Console.WriteLine(&lt;span class="str"&gt;"Meeting ended"&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FinishCode()
    {
        Thread.Sleep(1000);
        Console.WriteLine(&lt;span class="str"&gt;"Code complete"&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;p&gt;Finally, there is one other strategy to use.  Suppose you are working on a customer presentation.  You're ready to go, but you can't send it off to the client until your finance department finishes running some numbers for you.  You ask them to let you know as soon as that is complete, so you can send off the presentation.  The distinction here is that you don't really have any simultaneous work to be doing.  You just need to wait on them, and then kick off the email.  We'll start with the code:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Presentation
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; DataTable WaitForFinanceHandler();
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        Console.WriteLine(&lt;span class="str"&gt;"Tell Finance to finish, and get back to me."&lt;/span&gt;);
        WaitForFinanceHandler waitForFinance = FinishFinanceNumbers;
        Console.WriteLine(&lt;span class="str"&gt;"When done, take me to the SendEmail function"&lt;/span&gt;);
        waitForFinance.BeginInvoke(SendEmail, &lt;span class="kwrd"&gt;null&lt;/span&gt;);
        Console.ReadLine();
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendEmail(IAsyncResult result)
    {
        WaitForFinanceHandler waitForFinance =
            (WaitForFinanceHandler) ((AsyncResult) result).AsyncDelegate;
        DataTable financeResults = waitForFinance.EndInvoke(result);
        Console.WriteLine(&lt;span class="str"&gt;"Finance Results received, emailing..."&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; DataTable FinishFinanceNumbers()
    {
        Thread.Sleep(2000);
        Console.WriteLine(&lt;span class="str"&gt;"Finance team finished, returning results"&lt;/span&gt;);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; DataTable();
    }
}&lt;/pre&gt;
&lt;p&gt; So there are a few minor changes here.  First, rather than wait on the worker thread to complete in Main, we've opted to ask the thread to notify us by calling back to a different method, SendEmail.  Perhaps you've noticed that Intellisense has indicated 2 parameters to BeginInvoke that we have not previously used when calling BeginInvoke.  The first of these is actually another delegate that represents the method to call when the work completes.  The signature for this method is hard coded.  &lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; METHOD_NAME(IAsyncResult result)&lt;/pre&gt;
&lt;p&gt;Things get a bit more complicated here.  You see, you need to be able to recover the delegate that you declared in Main, so that you can call EndInvoke (thus getting the function's return value), as we've done in our previous examples.  This is a two step process, and it isn't intuitive.  The property you are looking for is AsyncDelegate, but the IAsynResult interface does not define it.  2 steps are needed in order to get to your delegate:&lt;/p&gt;
&lt;p&gt;1. Add this using statement to your code:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Runtime.Remoting.Messaging;&lt;/pre&gt;
&lt;p&gt;2. You will need to cast the IAsyncResult to an AsyncResult object.  Finally, you will then cast the AsyncDelegate property to a delegate of your type.  &lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;WaitForFinanceHandler waitForFinance =
   (WaitForFinanceHandler) ((AsyncResult) result).AsyncDelegate;&lt;/pre&gt;
&lt;p&gt;Certainly there is a reason for this headache, but with Generics available, I don't see it off the top of my head.  At any rate, once you have the original delegate, and the IAsyncResult, you have the same tools as in the previous example.  A call to EndInvoke will yield your result.&lt;/p&gt;
&lt;p&gt;One last thing.  The code above works, but only just barely.  Recall that using delegates for asynchronous calls uses the Threadpool behind the scenes.  Further, recall that the Threadpool's Threads are all Background threads.  Therefore, when the foreground thread (Main) completes, the background thread is killed.  So if I had not placed Console.ReadLine() at the very end, the program would have ended without waiting for finance or sending my report at all.  &lt;/p&gt;
&lt;p&gt;That's it for delegates right now.  There are a few more things to learn, but my goal here was not to repeat what I had already read, but to try and present the basic concepts in a manner that was more tangible.  If the concepts now make sense, the rest of the articles on the net will hopefully come more naturally.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/19.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/15/19.aspx</guid>
            <pubDate>Sun, 15 Apr 2007 04:02:27 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/19.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/15/19.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/19.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/19.aspx</trackback:ping>
        </item>
        <item>
            <title>Threading: Using Delegates - Part 1</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/14/17.aspx</link>
            <description>&lt;p&gt;.Net provides a number of ways to manage threading, and the Internet has no shortage of articles explaining how to use them.  Unfortunately, I still found myself having to look up examples every time I was going to use threads of any sort.  The information wasn't sticking, because I didn't fundamentally understand what was going on.  The goal of this is to present the technology in a more intuitive way.&lt;/p&gt; &lt;p&gt;As mentioned, there are a number of ways to handle threading in .net.  I'll hopefully get to all of them in time, but I'm starting with delegates for two reasons:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;&lt;font color="#555555"&gt;They're used heavily throughout the framework&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font color="#555555"&gt;They're one of the most confusing concepts.&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Before I get started, make sure you're comfortable with what a thread is.  A brief google search led me to a decent article on this &lt;a href="http://www.informit.com/articles/article.asp?p=170919&amp;amp;rl=1" target="_blank"&gt;here&lt;/a&gt;.  The following two terms are also critical to understanding the rest of the post.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;ThreadPool &lt;/strong&gt;- Threads are fairly expensive to create and destroy.  You also need to be careful that you don't spawn too many at a time, because CPU time is consumed when switching between one thread and another.  As a result, the CLR manages a pool of threads for you.  This allows you to queue as much work as you want, trusting the CLR to manage things efficiently for you.  &lt;/li&gt;&lt;li&gt;&lt;strong&gt;Delegates -&lt;/strong&gt; It's easy to be mystified by how delegates work, because so much is done for you by the compiler behind the scenes.  To briefly summarize:  &lt;ul&gt; &lt;li&gt;A delegate is often described as a function pointer, a variable that represents a function.  This is a true, but not entirely incomplete description.   &lt;/li&gt;&lt;li&gt;Delegates are classes that inherit from System.MulticastDelegate.  &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;font color="#000000"&gt;It may help to think of your delegate declaration as being very similar to defining a variable of a generic class.  (eg: Delegate&amp;lt;RETURNTYPE, ARG1, ARG2&amp;gt; myDelegate).  The syntactic help you get is really smoke and mirrors provided by the compiler. &lt;/font&gt;&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MyDelegateType(&lt;span class="kwrd"&gt;object&lt;/span&gt; message);        
&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
{
    MyDelegateType myDelegateVariable = 
       &lt;span class="kwrd"&gt;new&lt;/span&gt; MyDelegateType(PrintToConsole.Print);

    &lt;span class="rem"&gt;// The following two lines do the exact same thing&lt;/span&gt;
    myDelegateVariable.Invoke(&lt;span class="str"&gt;"Hello"&lt;/span&gt;);
    myDelegateVariable(&lt;span class="str"&gt;"Hello"&lt;/span&gt;);
}&lt;/pre&gt;
&lt;h2&gt;Using Delegates for Asynchronous Execution&lt;/h2&gt;
&lt;p&gt;The BeginInvoke / EndInvoke pattern is repeated throughout the framework, so it's a good idea to at least understand this one, even if it doesn't wind up being your preferred strategy.&lt;/p&gt;
&lt;p&gt;Using BeginInvoke and EndInvoke follows a similar process to what happens when you go out to eat.  Lets introduce our characters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Customer - Our hapless calling method.  Likes steak. 
&lt;/li&gt;&lt;li&gt;Waitress - Our asynchronous delegate.  
&lt;/li&gt;&lt;li&gt;Cook - Our easily disgruntled, time consuming worker method. &lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Now, briefly consider what would happen if this process was a synchronous one.  Our customer comes in and orders food, but then, rather than talking on their cell at an obscene volume, or making decidedly half-hearted attempts to control their kids, they just freeze.  They don't move, they don't speak.  It's a peaceful place, and it makes the waitress' job easier, because all she has to do is bring the order to the cook.  There will be no need for breadsticks, or drink refills.  The cook takes the order, and begins working on the food.  It's done when it's done.  There's no hurry, and no complaint.  The cook makes the food, and puts it up when he's good and ready.  Our waitress immediately snatches it up, and brings it to the customer, who all of the sudden wakes up and begins eating, thinking how fast the service here is.&lt;/p&gt;
&lt;p&gt;This is how most of our code generally operates.  Generally, it makes sense to work in this way.  In this case it doesn't.  Below is code that works in a manner more consistent with your past restaurant experiences.  Take a look at the Console statements, as I'm trying to equate the code to the restaurant scenario.&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Cook
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;internal&lt;/span&gt; Dinner Make(Order order)
    {
        Console.WriteLine(&lt;span class="str"&gt;"\tCook: Start Cooking... "&lt;/span&gt; + order.foodOrder);
        Thread.Sleep(5000);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (order.customer.IsAnnoying)
            Console.WriteLine(&lt;span class="str"&gt;"\tCook: Applying special sauce..."&lt;/span&gt;);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Dinner();
    }
}
&lt;span class="kwrd"&gt;class&lt;/span&gt; RestaurantProgram
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;delegate&lt;/span&gt; Dinner WaitressHandler(Order order);
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
    {
        Console.WriteLine(&lt;span class="str"&gt;"Customer enters the restaurant"&lt;/span&gt;); 
        Customer customer = &lt;span class="kwrd"&gt;new&lt;/span&gt; Customer();

        Console.WriteLine(&lt;span class="str"&gt;"A waitress comes over to take the customer's order."&lt;/span&gt;);            
        WaitressHandler customersWaitress = &lt;span class="kwrd"&gt;new&lt;/span&gt; WaitressHandler(Cook.Make);

        Console.WriteLine(&lt;span class="str"&gt;"Customer: I'd like the steak and potatoes please."&lt;/span&gt;);            
        Order order = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order(&lt;span class="str"&gt;"Steak and Potatoes"&lt;/span&gt;, customer);

        Console.WriteLine(&lt;span class="str"&gt;"Waitress takes your order, and brings it to the cook."&lt;/span&gt;);
        IAsyncResult result = customersWaitress.BeginInvoke(order, &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;);

        &lt;span class="kwrd"&gt;int&lt;/span&gt; numMinutesWaited = 0;  &lt;span class="rem"&gt;// minutes = seconds...&lt;/span&gt;
        &lt;span class="kwrd"&gt;while&lt;/span&gt;(!result.IsCompleted)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (numMinutesWaited == 0)
                Console.WriteLine(&lt;span class="str"&gt;"Customer commences people-watching.  Drips salsa all over the table."&lt;/span&gt;);

            &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt;(numMinutesWaited == 1)
                Console.WriteLine(&lt;span class="str"&gt;"Customer is out of chips, running short of patience."&lt;/span&gt;);

            &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (numMinutesWaited == 2)
            {
                Console.WriteLine(&lt;span class="str"&gt;"Customer complains about the wait...  such a bad idea."&lt;/span&gt;);
                customer.IsAnnoying = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
            }

            numMinutesWaited += 1;
            Thread.Sleep(1000);
        }

        Console.WriteLine(&lt;span class="str"&gt;"Food is prepared, and the waitress brings it to the customer"&lt;/span&gt;);
        Dinner dinner = customersWaitress.EndInvoke(result);

        Console.ReadLine();
    }
}&lt;/pre&gt;
&lt;p&gt;And the output:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jeffdeville.com/blog/images/jeffdeville_com/blog/WindowsLiveWriter/ThreadingUsingDelegatesPart1_11B9E/RestaurantOrderOutput21.jpg" atomicselection="true"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="173" src="http://jeffdeville.com/blog/images/jeffdeville_com/blog/WindowsLiveWriter/ThreadingUsingDelegatesPart1_11B9E/RestaurantOrderOutput_thumb12.jpg" width="600" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Ok, let's take a look at what our waitress is doing.  First, she takes the order, using BeginInvoke.  Using this method places the customer's order in a queue (ThreadPool) for the cook.  You can think of this as the cook's grill.  He can only cook so many things at a time.  &lt;/p&gt;
&lt;p&gt;What is that IAsyncResult object?  This object helps us keep track of the cook's progress.  In this code, we check the IsCompleted property to see if the food is ready.  As you can see, every second of delay increases our customer's agitation.  Eventually, IsComplete will be true, meaning that dinner is ready.  When this happens, we fall out of the while loop, and the waitress bring our customer's food over with the EndInvoke method.  The result object we pass in to EndInvoke is akin to the tickets you see posted over a completed meal on the Ready counter.  It's what tells our waittress whose dinner has just been finished, which allows her to determine which customer it belongs to.  &lt;/p&gt;
&lt;p&gt;Hopefully, this analogy helps explain what delegate are for, and provides some insight into how they work.  I'll go over alternative ways to handle your asynchronous code with delegates in Part 2.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/17.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/14/17.aspx</guid>
            <pubDate>Sun, 15 Apr 2007 00:12:08 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/17.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/14/17.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/17.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/17.aspx</trackback:ping>
        </item>
        <item>
            <title>Threading: Lock == Monitor</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/14/14.aspx</link>
            <description>&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The lock statement compiles down to the static Monitor class.  In other words:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;lock&lt;/span&gt;(&lt;span class="kwrd"&gt;[SOME OBJECT]&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;  &lt;span class="rem"&gt;// execute code&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;.. is equivalent to&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;Monitor.Enter(&lt;span class="kwrd"&gt;[SOME OBJECT]&lt;/span&gt;)&lt;br /&gt;&lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  &lt;span class="rem"&gt;// execute code&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;span class="kwrd"&gt;finally&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  Monitor.Exit(&lt;span class="kwrd"&gt;[SOME OBJECT]&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;The monitor class provides more detailed control, but it's useful to know that for simple cases like the one above, they amount to the same thing.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/14.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/14/14.aspx</guid>
            <pubDate>Sat, 14 Apr 2007 19:02:59 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/14.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/14/14.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/14.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/14.aspx</trackback:ping>
        </item>
        <item>
            <title>Threading: Foreground vs Background</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/14/13.aspx</link>
            <description>&lt;p&gt;So I just had a very &lt;strong&gt;VERY&lt;/strong&gt; difficult phone screen the other day.  It was a great experience though, because it's pointed me in the direction of a number of important topics that I'm weak on.  So this will be a series on those topics.  Hopefully round 2 will go a little smoother.  :-)  &lt;/p&gt; &lt;p&gt;At any rate, my first topic is threading.  I took an Operating Systems class in school, but it was &lt;/p&gt; &lt;ol&gt; &lt;li&gt;In C  &lt;/li&gt;&lt;li&gt;Longer ago than I like to consider. &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Definitely time to update my knowledge in this area&lt;/p&gt; &lt;h2&gt;Foreground / Background Threads - What's the Difference?:  &lt;/h2&gt; &lt;p&gt;The CLR doesn't close your program until all the foreground threads have ended.  On the other hand, background threads are viewed as only having relevance while foreground threads are running.  As a result, when the foreground threads end, your program ends.  The CLR will kill any background threads that were executing as necessary.  What's the code distinction? &lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;[THREAD].IsBackground = &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;To illustrate, below is a heavily plagiarized code snippet from &lt;a href="http://www.amazon.com/exec/obidos/ASIN/1590598237/shelfari-20" target="_blank"&gt;Pro C# with .Net 3.0 Primer&lt;/a&gt;, (page 467).&lt;/p&gt;
&lt;p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
{
   Thread bgroundThread = 
     &lt;span class="kwrd"&gt;new&lt;/span&gt; Thread(&lt;span class="kwrd"&gt;new&lt;/span&gt; ThreadStart(SomeFunction));
   bgroundThread.IsBackground = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
   bgroundThread.Start();
}&lt;/pre&gt;
&lt;p&gt;Because bgroundThread is a background thread, this console application will end immediately, and whatever you have called in SomeFunction will not be executed.  By default, threads are created as foreground threads, so commenting out the IsBackground = true; line will mean the console application doesn't end until SomeFunction completes its work and returns.&lt;/p&gt;
&lt;h2&gt;What about the ThreadPool / BeginInvoke?&lt;/h2&gt;
&lt;p&gt;Threadpool threads &lt;strong&gt;are background threads&lt;/strong&gt;, which is the opposite of the behavior you can expect from generating the Thread yourself.  Further, it's important to realize that when you create a delegate, and use BeginInvoke, you are not creating a thread, but queueing the work on the thread pool, which again means you're running on a background thread.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/13.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/14/13.aspx</guid>
            <pubDate>Sat, 14 Apr 2007 19:02:29 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/13.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/14/13.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/13.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/13.aspx</trackback:ping>
        </item>
        <item>
            <title>Visual Studio 2005 Snippet Editor</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/13/11.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://timheuer.com/blog/archive/2007/04/12/14039.aspx" target="_blank"&gt;Tim Heuer&lt;/a&gt;, as usual, is putting me in the know.  It appears that my complaint regarding Visual Studio's lack of a code snippet editor is being rectified by the community.  Check out:&lt;/p&gt; &lt;p&gt;&lt;a title="Snippy" href="http://www.gotdotnet.com/codegallery/codegallery.aspx?id=b0813ae7-466a-43c2-b2ad-f87e4ee6bc39" target="_blank"&gt;Snippy&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A quick note on installation:  No shortcuts are placed anywhere, and by default the program files are installed to: C:\Program Files\PowerToys for Visual Studio 8\Snippy.  If you'd like to be able to launch this from within Visual Studio, you can do so easily:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;&lt;font color="#555555"&gt;In VS2005, Tools &amp;gt;&amp;gt; External Tools&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font color="#555555"&gt;Click Add&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font color="#555555"&gt;Title: Snippy (or whatever you want)&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font color="#555555"&gt;Command: C:\Program Files\PowerToys for Visual Studio 8\Snippy\Snippy.exe&lt;/font&gt;&lt;/li&gt; &lt;li&gt;&lt;font color="#555555"&gt;Click Ok&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Unfortunately, it doesn't yet seem to know where your Code Snippets directory is to help you out w/ opening and saving, and I couldn't pass that directory in as an Argument or an Initial Directory.  &lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/11.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/13/11.aspx</guid>
            <pubDate>Fri, 13 Apr 2007 14:47:38 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/11.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/13/11.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/11.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/11.aspx</trackback:ping>
        </item>
        <item>
            <title>Making the Generic Collections' delegate-based functions more useful</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/11/8.aspx</link>
            <description>&lt;p&gt;.Net 2.0's new generic collections are easy to love.  In addition to helping you easily eliminate boxing issues, they've also been bequeathed with some clever delegate-based find and action capabilities.  For instance, suppose you have a string collection, and want to implement a way to only pull values from that collection that start with a certain letter.  It won't take you much googling to come up with a solution that uses the Predicate&amp;lt;&amp;gt; delegate and looks something like this:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; StupidSearchMethod&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;static&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; names = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(&lt;br /&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"Jeff"&lt;/span&gt;, &lt;span class="str"&gt;"Tara"&lt;/span&gt;, &lt;span class="str"&gt;"Joe"&lt;/span&gt; });&lt;br /&gt;    [STAThread]&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; Main()&lt;br /&gt;    {&lt;br /&gt;        List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; namesThatStartWithJ = &lt;br /&gt;            names.FindAll(NameStartsWithJ);&lt;br /&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; namesThatStartWithJ)&lt;br /&gt;            Console.WriteLine(s);&lt;br /&gt;        Console.ReadLine();&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;bool&lt;/span&gt; NameStartsWithJ(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; input.StartsWith(&lt;span class="str"&gt;"J"&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img width="100" height="57" border="0" src="http://jeffdeville.com/blog/images/jeffdeville_com/blog/WindowsLiveWriter/MakingtheGenericCollectionsdelegatebased_132C/FindAllOutput1%5B5%5D.jpg" style="border-width: 0px;" alt="" /&gt; &lt;/p&gt;
&lt;p&gt;Well that's ok, but am I really going to have to write a new search method when I want to find the strings that start with 'T'?  It would seem so at first glance, because I am not allowed to pass any information into my predicate to help my search method.  Fortunately, this is a pretty simple search, and would be amenable to using an anonymous method:&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AnonymousSearchMethod&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;static&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; names = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(&lt;br /&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"Jeff"&lt;/span&gt;, &lt;span class="str"&gt;"Tara"&lt;/span&gt;, &lt;span class="str"&gt;"Joe"&lt;/span&gt; });&lt;br /&gt;&lt;br /&gt;    [STAThread]&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; Main()&lt;br /&gt;    {&lt;br /&gt;        List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; namesThatStartWithJ = names.FindAll(&lt;br /&gt;            &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; input) { &lt;span class="kwrd"&gt;return&lt;/span&gt; input.StartsWith(&lt;span class="str"&gt;"J"&lt;/span&gt;); });&lt;br /&gt;&lt;br /&gt;        Console.WriteLine(&lt;span class="str"&gt;"Names that start with 'J':"&lt;/span&gt;);&lt;br /&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; namesThatStartWithJ)&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\t"&lt;/span&gt; + s);&lt;br /&gt;&lt;br /&gt;        List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; namesThatStartWithT = names.FindAll(&lt;br /&gt;            &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; input) { &lt;span class="kwrd"&gt;return&lt;/span&gt; input.StartsWith(&lt;span class="str"&gt;"T"&lt;/span&gt;); });&lt;br /&gt;&lt;br /&gt;        Console.WriteLine(&lt;span class="str"&gt;"Names that start with 'T':"&lt;/span&gt;);&lt;br /&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; namesThatStartWithT)&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\t"&lt;/span&gt; + s);&lt;br /&gt;        Console.ReadLine();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img width="214" height="94" border="0" src="http://jeffdeville.com/blog/images/jeffdeville_com/blog/WindowsLiveWriter/MakingtheGenericCollectionsdelegatebased_132C/FindAllOutput2%5B9%5D.jpg" style="border-width: 0px;" alt="" /&gt; &lt;/p&gt;
&lt;p&gt;But what if my search logic was not so trivial that I could afford to duplicate it over and over?  Suppose I am looking for strings that start with a certain letter, but also have two or more vowels?  Now I really need to be able to pass in extra parameters, or I'm going to just have to give up on this entirely, and code my own method.  Fortunately, I can combine a more flexible search method with anonymous methods to get what I want.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AnonymousPlusComplexSearchMethod&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;static&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; names = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;(&lt;br /&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;[] { &lt;span class="str"&gt;"Jeff"&lt;/span&gt;, &lt;span class="str"&gt;"Tara"&lt;/span&gt;, &lt;span class="str"&gt;"Joe"&lt;/span&gt; });&lt;br /&gt;&lt;br /&gt;    [STAThread]&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; Main()&lt;br /&gt;    {&lt;br /&gt;        List&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; results = names.FindAll(&lt;br /&gt;            &lt;span class="kwrd"&gt;delegate&lt;/span&gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; input)&lt;br /&gt;                {&lt;br /&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; CheckStartLetterAndNumVowels(input, &lt;span class="str"&gt;"J"&lt;/span&gt;, 2);&lt;br /&gt;                });&lt;br /&gt;&lt;br /&gt;        &lt;span class="rem"&gt;// Show Results&lt;/span&gt;         Console.WriteLine(&lt;span class="str"&gt;"Start with 'J' and have 2 vowels:"&lt;/span&gt;);&lt;br /&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt; s &lt;span class="kwrd"&gt;in&lt;/span&gt; results)&lt;br /&gt;            Console.WriteLine(&lt;span class="str"&gt;"\t"&lt;/span&gt; + s);&lt;br /&gt;        Console.ReadLine();&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; List&amp;lt;&lt;span class="kwrd"&gt;char&lt;/span&gt;&amp;gt; vowels = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;char&lt;/span&gt;&amp;gt;(&lt;br /&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;char&lt;/span&gt;[] { &lt;span class="str"&gt;'a'&lt;/span&gt;, &lt;span class="str"&gt;'e'&lt;/span&gt;, &lt;span class="str"&gt;'i'&lt;/span&gt;, &lt;span class="str"&gt;'o'&lt;/span&gt;, &lt;span class="str"&gt;'u'&lt;/span&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;bool&lt;/span&gt; CheckStartLetterAndNumVowels(&lt;br /&gt;        &lt;span class="kwrd"&gt;string&lt;/span&gt; input, &lt;span class="kwrd"&gt;string&lt;/span&gt; startLetter, &lt;span class="kwrd"&gt;int&lt;/span&gt; numVowels)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt; (!input.StartsWith(startLetter))&lt;br /&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; input.Length; i++)&lt;br /&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (vowels.Contains(input[i]))&lt;br /&gt;                numVowels--;&lt;br /&gt;&lt;br /&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; numVowels == 0;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img width="315" height="64" border="0" src="http://jeffdeville.com/blog/images/jeffdeville_com/blog/WindowsLiveWriter/MakingtheGenericCollectionsdelegatebased_132C/FindAllOutput3%5B3%5D.jpg" style="border: 0px none ;" alt="" /&gt; &lt;/p&gt;
&lt;p&gt;This strategy can be used with the Action&amp;lt;&amp;gt; delegates, like ForEach(Action&amp;lt;&amp;gt;) as well.&lt;/p&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/8.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/11/8.aspx</guid>
            <pubDate>Wed, 11 Apr 2007 05:23:20 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/8.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/11/8.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/8.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/8.aspx</trackback:ping>
        </item>
        <item>
            <title>More ReSharper Live Templates</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/09/6.aspx</link>
            <description>&lt;p&gt;So just dump mode now because these snippets are a bit random.  First the download link again: &lt;a href="http://www.jeffdeville.com/downloads/jeffdeville_resharper_livetemplates.xml"&gt;Live Templates Download&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;NUnit / MbUnit&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;test&lt;/strong&gt; - for some reason, I don't have this snippet using VS, but it was so easy to just create one, I didn't bother digging around to figure out where it was&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;[Test]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; $METHOD$Test()&lt;br /&gt;{&lt;br /&gt;    $END$&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;teste&lt;/strong&gt; - Yeah so I probably could have named this something else, but I just didn't care enough.  :-)  &lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;[Test,ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;($EXCEPTION$))]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; $METHOD$_Exception_Test()&lt;br /&gt;{&lt;br /&gt;    $END$&lt;br /&gt;}&lt;/pre&gt;
&lt;h3&gt;Properties&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;propcint&lt;/strong&gt; - Create an int property that looks for it's value from app/web.config before using a default value.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _$PROPVAR$ = $DEFAULT$;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; $PROPNAME$&lt;br /&gt;{&lt;br /&gt;    get&lt;br /&gt;    {&lt;br /&gt;     &lt;span class="kwrd"&gt;if&lt;/span&gt; (!&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(&lt;br /&gt;         ConfigurationManager.AppSettings[&lt;span class="str"&gt;"$CLASSNAME$_$PROPNAME$"&lt;/span&gt;]))&lt;br /&gt;        _$PROPVAR$ = Convert.ToInt32(&lt;br /&gt;           ConfigurationManager.AppSettings[&lt;span class="str"&gt;"$CLASSNAME$_$PROPNAME$"&lt;/span&gt;]);&lt;br /&gt;     &lt;span class="kwrd"&gt;return&lt;/span&gt; _$PROPVAR$;&lt;br /&gt;    }&lt;br /&gt;    set { _$PROPVAR$ = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }&lt;br /&gt;}&lt;/pre&gt;
&lt;h3&gt;LLBL - Adapter&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;ec&lt;/strong&gt; - Create an entity collection. This is pretty simple, but I find that the only drawback to using llblgen is that I tend to have fairly long type names.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;EntityCollection&amp;lt;$Type$&amp;gt; $varname$ = &lt;span class="kwrd"&gt;new&lt;/span&gt; EntityCollection&amp;lt;$Type$&amp;gt;();&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;llu&lt;/strong&gt; - Creates a DataAccessAdapter inside of a using block.&lt;/p&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt;(DataAccessAdapter adapter = &lt;span class="kwrd"&gt;new&lt;/span&gt; DataAccessAdapter())&lt;br /&gt;{&lt;br /&gt;     $END$&lt;br /&gt;}&lt;/pre&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/6.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/09/6.aspx</guid>
            <pubDate>Tue, 10 Apr 2007 00:11:08 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/6.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/09/6.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/6.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/6.aspx</trackback:ping>
        </item>
        <item>
            <title>ReSharper Live Templates (Code Snippets) - Singleton</title>
            <link>http://jeffdeville.com/blog/archive/2007/04/09/5.aspx</link>
            <description>&lt;p&gt;I'll be posting a series of Live Templates I've created, as JetBrains has not yet created a community site where we can share and import directly from Visual Studio yet. (hint, hint).&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Singleton&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;First a quick note.  The obvious way to implement singletons in c# is not thread safe:&lt;/p&gt; &lt;p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton instance=&lt;span class="kwrd"&gt;null&lt;/span&gt;;

    Singleton()
    {
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton Instance
    {
        get
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance==&lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();
            }
            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt; For an excellent discussion on the topic, read &lt;a title="Jon Skeet on Implementing Singletons in c#" href="http://www.yoda.arachsys.com/csharp/singleton.html"&gt;Jon Skeet&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I based my template on his solution 4:&lt;/p&gt;
&lt;p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;sealed&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Singleton
{
    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; Singleton instance=&lt;span class="kwrd"&gt;new&lt;/span&gt; Singleton();

    &lt;span class="rem"&gt;// Explicit static constructor to tell C# compiler&lt;/span&gt;
    &lt;span class="rem"&gt;// not to mark type as beforefieldinit&lt;/span&gt;
    &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton()
    {
    }

    Singleton()
    {
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Singleton Instance
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;This, and my other currently used Live Templates can be had here (right click, Save): &lt;a href="http://www.jeffdeville.com/downloads/jeffdeville_resharper_livetemplates.xml"&gt;Live Templates Download&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="preproc"&gt;#region&lt;/span&gt; Singleton

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; $ClassName$ _instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; $ClassName$();

&lt;span class="kwrd"&gt;static&lt;/span&gt; $ClassName$(){}
&lt;span class="kwrd"&gt;private&lt;/span&gt; $ClassName$(){}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; $ClassName$ Instance
{
    get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _instance; }
}

&lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://jeffdeville.com/blog/aggbug/5.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jeff Deville</dc:creator>
            <guid>http://jeffdeville.com/blog/archive/2007/04/09/5.aspx</guid>
            <pubDate>Mon, 09 Apr 2007 22:58:09 GMT</pubDate>
            <wfw:comment>http://jeffdeville.com/blog/comments/5.aspx</wfw:comment>
            <comments>http://jeffdeville.com/blog/archive/2007/04/09/5.aspx#feedback</comments>
            <wfw:commentRss>http://jeffdeville.com/blog/comments/commentRss/5.aspx</wfw:commentRss>
            <trackback:ping>http://jeffdeville.com/blog/services/trackbacks/5.aspx</trackback:ping>
        </item>
    </channel>
</rss>