<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devplanet.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/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel><title>devplanet.com</title><link>http://devplanet.com/blogs/</link><description>a community devoted to .NET and MS SQL developers</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30912.2823)</generator><item><title>Inline Asynchronous UI Coding using IDisposable in C# .NET</title><link>http://devplanet.com/blogs/brianr/archive/2008/12/15/async-coding-using-idisposable.aspx</link><pubDate>Mon, 15 Dec 2008 17:00:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:14089</guid><dc:creator>Brian Rudolph</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I recently watched this video on the PowerThreading library: &lt;a href="http://blogs.msdn.com/charlie/archive/2008/12/03/jeff-richter-video-on-asynchronous-programming-and-his-power-threading-library.aspx"&gt;http://blogs.msdn.com/charlie/archive/2008/12/03/jeff-richter-video-on-asynchronous-programming-and-his-power-threading-library.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is very interesting as Jeff Richter utilizes a strange behavior in the way the C# compiler works with IEnumerables to make a useful and easy-to-use async library.&amp;nbsp; His primary goal was to allow a website\service yield processing during I/O operations, which can provide for better scalability in your app tier.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;While this is quite interesting, I personally would avoid using strange compilation patterns to my advantage in an application.&amp;nbsp; Specifically because you have no assurance that the compiler will always do this as the language and subsequent compilers evolve.&amp;nbsp; What I was interested in was the mechanism for including asynchronous calls into code without using a complex series of callbacks.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Now, to be quite fair, Jeff&amp;#39;s implementation was an attempt to limit thread spawning, and context switches.&amp;nbsp; I am not going to account for this.&amp;nbsp; I am more interested in parrallel processing.&lt;/p&gt;
&lt;p&gt;Let me give you an example.&amp;nbsp; Take for instance a series of objects that the UI is going to take cues from and make subsequent database calls to retrieve extra data for.&amp;nbsp; A simple example is a treeview.&amp;nbsp; If I have a bunch of nodes in a tree and the UI requires that I display the child node or&amp;nbsp;items&amp;nbsp;count for each of these nodes. What you would generally do is loop through the objects and call the DB to retrieve the data,&amp;nbsp;and assign the UI components in one serial block of code.&amp;nbsp; The problem with this is that the database is more than likely capable of servicing the request for numerous nodes simultaneously, but in the UI we are implementing this serially.&amp;nbsp; If the tree structure is complex, and you are showing more than just a node count, it is plausible that each of these requests will take upward of 50ms.&amp;nbsp; If you have several dozen nodes being displayed this can easily add seconds to your response time.&lt;/p&gt;
&lt;p&gt;A serial block of code might look like this:&lt;/p&gt;
&lt;p&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;TreeNodeCollection nodes &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; TreeNodeCollection();
&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;foreach&lt;/span&gt; (TreeNode node &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;in&lt;/span&gt; nodes)
{
     &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; objectCount &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; database.DoSomething((MyObject)node.Tag); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//call the database&lt;/span&gt;
     node.Text &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; String.Format(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;{0} ({1})&amp;quot;&lt;/span&gt;, node.Text, objectCount); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//update the UI&lt;/span&gt;
 }&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Simple and clean, but slow.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Why not do all of these calls asynchronously, wait for all of the results to filter in, and then update the UI?&amp;nbsp; This should surely be faster than a serial operation.&amp;nbsp; Let&amp;#39;s say, we spawn&amp;nbsp;Math.Min(nodes.Count, 10)&amp;nbsp;threads to accomplish this&amp;nbsp;task.&amp;nbsp; So at a maximum, we will use 10 threads to do the work. If we have 20 nodes, it should take at the longest 2 times as long as the longest running call.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The usual issue with this is that it requires you to use several call-back methods, do complicated thread monitoring, and subsequently put the UI into a wait state until all of the external threads have finished.&amp;nbsp; Unfortunately there just is no simple way to do it.&lt;/p&gt;
&lt;p&gt;So I got to thinking, what construct can I use to signal an asynchronous block, that would prevent execution beyond it&amp;#39;s bounds until all the threads were done.&amp;nbsp; The answer, IDisposable.&amp;nbsp; If I created an IDisposable object and wrapped it in a &amp;quot;using&amp;quot; I could prevent the Dispose method from exiting until all the threads have finished.&lt;/p&gt;
&lt;p&gt;Utilizing the rest of my Threading objects, this should be fairly simple to accomplish, all I had to do was add a done flag to my workerbee:&lt;/p&gt;
&lt;p&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; AsyncBlock : IDisposable
{


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;long&lt;/span&gt; timeoutMS &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 0;
        Dispatcher dispatcher &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; started &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;;

        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; AsyncBlock() : &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;(10) { }

        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; AsyncBlock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; timeoutSeconds) : &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;(timeoutSeconds, 5) { }

        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; AsyncBlock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; timeoutSeconds, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; threads) : &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Dispatcher(threads), timeoutSeconds) { }

        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; AsyncBlock(Dispatcher dispatcher, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; timeoutSeconds)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.dispatcher &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; dispatcher;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.timeoutMS &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; timeoutSeconds &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;*&lt;/span&gt; 1000;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; List&amp;lt;WorkerBee&amp;gt;();
        }


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; IList&amp;lt;WorkerBee&amp;gt; WorkQueue
        {
            get;
            set;
        } 


       &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; IList&amp;lt;WorkerBee&amp;gt; GetWorkers()        
       {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; List&amp;lt;WorkerBee&amp;gt;(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue);
        } 


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; AddToQueue(WorkerBee bee)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue.Add(bee);
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.dispatcher.AddToQueue(bee);
            started &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
        }


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Start()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;foreach&lt;/span&gt; (WorkerBee bee &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;in&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue)
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (!bee.Done)
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.dispatcher.AddToQueue(bee);


            started &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
        }


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Dispose()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (!started &amp;amp;&amp;amp; WorkQueue.Count !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 0)
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Exception(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;Work was never started&amp;quot;&lt;/span&gt;);
            }


            System.Diagnostics.Stopwatch watch &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; System.Diagnostics.Stopwatch();
            watch.Start();
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; done &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;;

            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;while&lt;/span&gt; (!done)
            {
                done &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;foreach&lt;/span&gt; (WorkerBee bee &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;in&lt;/span&gt; WorkQueue)
                {
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (!(done &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; done &amp;amp;&amp;amp; bee.Done))
                        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;break&lt;/span&gt;;
                }
 


                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (watch.ElapsedMilliseconds &amp;gt; timeoutMS)
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; TimeoutException(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;Timeout elapsed&amp;quot;&lt;/span&gt;);
 
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (!done)
                    Thread.Sleep(1); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;// wait 1ms to try again&lt;/span&gt;
            }
 
            watch.Stop();
        }
}


&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; DatabaseWorkerBee&amp;lt;RefType, ReturnType&amp;gt; : AnyWorkerBee&amp;lt;Delegate&amp;gt;
{
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;delegate&lt;/span&gt; ReturnType DBDelegate();
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; DBDelegate dbDelegate;

        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; ReturnType ReturnValue
        {
            get;
            set;
        } 


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; RefType ReferenceObject
        {
            get;
            set;
        } 


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; DatabaseWorkerBee(RefType refObject, DBDelegate del)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.ReferenceObject &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; refObject;
            dbDelegate &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; del;
        } 


        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;override&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; WorkStart()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;base&lt;/span&gt;.WorkStart();
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (dbDelegate !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
            {
                ReturnValue &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; dbDelegate();
            }
        }
    }


 &lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Alright, this is now a fairly simple implementation in&amp;nbsp; the UI:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;IList&amp;lt;WorkerBee&amp;gt; workers &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;;


&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (AsyncBlock block &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; AsyncBlock(30, Math.Min(nodes.Count, 10))) &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;// do these async with a 30 second timeout&lt;/span&gt;
{
      &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;foreach&lt;/span&gt; (TreeNode o &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;in&lt;/span&gt; nodes)
      {
          TreeNode node &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; o; &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//must declare this in the loop, or you get the same object in every worker, this is a documented closure bug in the framework&lt;/span&gt;
          block.AddToQueue(
              &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; DatabaseWorkerBee&amp;lt;TreeNode, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt;&amp;gt;( node, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;delegate&lt;/span&gt; { &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/* Do Database Call Here using the node object*/&lt;/span&gt;; }));
       }


      workers &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; block.GetWorkers()


}&lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//using will not return until all workers have been processed.&lt;/span&gt;


&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;foreach&lt;/span&gt; (WorkerBee worker &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;in&lt;/span&gt; workers)
{
      DatabaseWorkerBee&amp;lt;TreeNode, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt;&amp;gt; dbworker &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; worker &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;as&lt;/span&gt; DatabaseWorkerBee&amp;lt;TreeNode, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt;&amp;gt;;
      dbWorker.ReferenceObject.Text &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; String.Format(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;{0} ({1})&amp;quot;&lt;/span&gt;, dbWorker.ReferenceObject.Text, dbWorker.ReturnValue); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//update the UI&lt;/span&gt;
}


 &lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Woot!&amp;nbsp; All of the nodes are executed async, and all of the values are returned properly.&amp;nbsp; One thing to note, notice I am declaring a redundant TreeNode object inside the loop.&amp;nbsp; This is necessary, since we are using an anonymous method inside of the loop.&amp;nbsp; If we were to just reference the &amp;quot;o&amp;quot; in the iterator, we would get the same object passed to each anonymous method.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;My Threading Objects can be found here: &lt;a href="http://devplanet.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/brianr.Code+Snippets/ThreadingLib.zip"&gt;ThreadLib.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;[Edit]&lt;br /&gt;After further review and testing this code works quite well, except in the instance of windows authenticated database connections.&amp;nbsp; Because we are spawning new threads, and your security context is thread based, all of the subsequent database calls were made using the applications default user.&amp;nbsp; This isn&amp;#39;t a problem in Windows UI&amp;#39;s but in Web apps this is not good.&amp;nbsp; It could be as simple as setting the new threads to the same security context and the spawning thread, but I&amp;#39;m not sure I want that to happen for every thread.&amp;nbsp; I will work this out and do an update.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=14089" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/brianr/archive/tags/Threading/default.aspx">Threading</category><category domain="http://devplanet.com/blogs/brianr/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>PASS Community Summit Recap</title><link>http://devplanet.com/blogs/memydatabaseandi/archive/2008/12/03/pass-community-summit-recap.aspx</link><pubDate>Thu, 04 Dec 2008 01:18:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:13966</guid><dc:creator>Chuck</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;This year was my last year as a member of the leadership of the Professional Association for SQL Server.&amp;nbsp; I will still be involved with the organization, but will be rolling off of the Board of Directors at the end of my 2 year term in December.&lt;/p&gt;
&lt;p&gt;That made this event a little different for me.&amp;nbsp; I&amp;#39;ve been at 9 of the 10 US PASS conferences - the only one that&amp;nbsp;I missed was the first one.&amp;nbsp; I&amp;#39;ve attended them in a variety of capacities - speaker, board member and even general attendee.&lt;/p&gt;
&lt;p&gt;This year was the largest PASS conference ever.&amp;nbsp; It included three amazing keynotes.&amp;nbsp; My favorite was given by Dr. David DeWitt.&amp;nbsp; Dr. DeWitt is a Technical Fellow with the Gray Systems Lab in Madison, WI (which is located just an hour&amp;nbsp;down the road from me).&amp;nbsp; The keynote contained no marketing - the entire presentation was about the theory and research that goes into building high performance scalable database systems.&amp;nbsp; It was one of the best technical lectures that I have ever attended.&lt;/p&gt;
&lt;p&gt;There were over 120 technical sessions at this conference - and I did manage to make it to a few of them.&amp;nbsp; One of my goals for the conference was to meet as many Microsoft employees as I could, and I think I did quite well for myself.&amp;nbsp; I also served as the moderator for a panel debate on the pros and cons of NULL.&amp;nbsp; The panel consisted of 5 SQL Server MVPs, and one guy&amp;nbsp;who should be one.&amp;nbsp; The session was well attended and the debate got quite heated at times.&amp;nbsp; Who would have thought that a debate about nothing would be so well received?&lt;/p&gt;
&lt;p&gt;Next year&amp;#39;s PASS conference is again going to be held in Seattle.&amp;nbsp; The dates are November 3 - 6, 2009.&amp;nbsp; I encourage anyone who is interested in the best SQL Server education for the money to attend.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s it for now...got to get back to installing MOSS and PerformancePoint - but that&amp;#39;s another blog post entirely.&lt;/p&gt;
&lt;p&gt;Chuck&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=13966" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/memydatabaseandi/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://devplanet.com/blogs/memydatabaseandi/archive/tags/PASS/default.aspx">PASS</category><category domain="http://devplanet.com/blogs/memydatabaseandi/archive/tags/MVP/default.aspx">MVP</category><category domain="http://devplanet.com/blogs/memydatabaseandi/archive/tags/Gray+Systems+Lab/default.aspx">Gray Systems Lab</category><category domain="http://devplanet.com/blogs/memydatabaseandi/archive/tags/NULL/default.aspx">NULL</category></item><item><title>More Threading Goodness!</title><link>http://devplanet.com/blogs/brianr/archive/2008/11/26/more-threading-goodness.aspx</link><pubDate>Wed, 26 Nov 2008 20:48:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:13616</guid><dc:creator>Brian Rudolph</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Threading - I write about it a lot, even though i would consider myself somewhat of a rookie when it comes to threading complexities.&lt;/p&gt;
&lt;p&gt;One of the challenges we often find when trying to incorporate threading into applications is managing work units, thread pools, and queues.&amp;nbsp; What I&amp;#39;ve ended up doing is writing a few helpers to accommodate just these things.&lt;/p&gt;
&lt;p&gt;To start, we need to manage a pool of available thread resources.&amp;nbsp; This pool needs to have a limit, and needs to be able to generate threads on the fly if necessary.&amp;nbsp; However, the need for resource pools is not limited to threading.&amp;nbsp; Consider the scenario where you have a web service of some sort, being WSE, FTP HTTP whatever.&amp;nbsp; There is a lot of overhead in opening and handshaking for these connections.&amp;nbsp; If I had say a list of 100k files I needed to upload, and wanted to do that asynchronously, I wouldn&amp;#39;t want to throw away the connection after every upload.&amp;nbsp; So I need to do simple connection pooling.&amp;nbsp; But I don&amp;#39;t want to open 10 connections immediately if I am only going to need 2 either, so this pool needs to be dynamic.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;To even start writing a pool, we need some sort of queuing class for resource storage, and in the threaded world, this needs to be thread safe.&lt;/p&gt;
&lt;p&gt;So a simple wrapper may look something like this:&lt;/p&gt;
&lt;p&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; ThreadSafeQueue&amp;lt;T&amp;gt;
    {
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//This is the internal queue that we are wrapping&lt;/span&gt;
        Queue&amp;lt;T&amp;gt; queue &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Queue&amp;lt;T&amp;gt;();
 
        [NonSerialized]
        ReaderWriterLockSlim objLock &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; Locks.GetLockInstance(LockRecursionPolicy.NoRecursion); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//setup the lock;&lt;/span&gt;
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; Count
        {
            get
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ReadOnlyLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
                {
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Count;
                }
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Clear()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; WriteLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Clear(); ;
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; Contains(T item)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ReadOnlyLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Contains(item);
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; TryDequeue(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;out&lt;/span&gt; T obj)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ReadLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Count !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 0)
                {
                    obj &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Dequeue();
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
                }
            }
 
            obj &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;default&lt;/span&gt;(T);
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;;
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; T Dequeue()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; WriteLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Dequeue();
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Enqueue(T item)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; WriteLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Enqueue(item);
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; T Peek()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ReadOnlyLock(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.objLock))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.queue.Peek();
            }
        }
    }&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Then we can move on to the Resource Pool, which we want to be generic so that it can serve up any kind of resource, be it a thread, a web service connection, a widget of any sort.&lt;/p&gt;
&lt;p&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; ResourcePool&amp;lt;T&amp;gt; : IDisposable
        where T : &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt;
    {
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;event&lt;/span&gt; EventHandler CreatedResource;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;delegate&lt;/span&gt; T CreateResourceDelegate();
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;delegate&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; CanReuseResourceDelegate(T obj);
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; ResourcePool(CreateResourceDelegate creatorDelegate, CanReuseResourceDelegate checkerDelegate)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxResourceCount &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 10;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.IterationTimeout &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 5;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxRetry &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 100;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Creator &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; creatorDelegate;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.CheckResource &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; checkerDelegate;
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; CreateResourceDelegate Creator { get; set; }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; CanReuseResourceDelegate CheckResource { get; set; }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Retreives a resource from the pool.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; T Get()
        {
            T obj &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;try&lt;/span&gt;
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; tries &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 0;
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;while&lt;/span&gt; (obj == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxRetry == 0 || tries++ &amp;lt;= &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxRetry))
                {
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (Queue.Count == 0)
                    {
                        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxResourceCount == 0 || (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxResourceCount &amp;gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.ActiveResourceCount))
                        {
                            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Creator == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; NullReferenceException(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;Unable to create new Resource Instance&amp;quot;&lt;/span&gt;);
 
                            obj &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Creator();
                            FireEvent(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;ref&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.CreatedResource);
                        }
                        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;else&lt;/span&gt;
                        {
                            Thread.Sleep(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.IterationTimeout); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;// wait for one to become available if we can&amp;#39;t create one.&lt;/span&gt;
                        }
                    }
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;else&lt;/span&gt;
                    {
                        obj &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; Queue.Dequeue();
                    }
                }
            }
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;lock&lt;/span&gt; (_ActiveResourceCountLock)
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.ActiveResourceCount++;
            }
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (obj == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;throw&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; NullReferenceException(&lt;span style="color:#666666;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&amp;quot;Unable to retrieve Resource Instance&amp;quot;&lt;/span&gt;);
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; obj;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Returns a resource to the pool.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;param name=&amp;quot;obj&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Release(T obj)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;try&lt;/span&gt;
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxResourceCount == 0 || Queue.Count &amp;lt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.MaxResourceCount)
                {
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.CheckResource == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt; || &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.CheckResource(obj))
                    {
                        Queue.Enqueue(obj);
                    }
                }
            }
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;lock&lt;/span&gt; (_ActiveResourceCountLock)
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.ActiveResourceCount--; &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//decrement counter so we know we can reuse the resource.&lt;/span&gt;
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; ActiveResourceCount { get; set; }
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;object&lt;/span&gt; _ActiveResourceCountLock &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Object();
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the maximum number of resources the pool will hold and subsequently attempt to create.  Default is 10.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Set to 0 enables no limit.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; MaxResourceCount { get; set; }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the maximum number of times the pool will look for or attempt to create a resource.  Default is 100.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Set to 0 enables no limit.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; MaxRetry { get; set; }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the length of time(in milliseconds) that the pool will wait between tries to get a resource. Default is 5ms.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; IterationTimeout { get; set; }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; ThreadSafeQueue&amp;lt;T&amp;gt; Queue
        {
            get { &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;._Queue; }
        }&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;readonly&lt;/span&gt; ThreadSafeQueue&amp;lt;T&amp;gt; _Queue &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ThreadSafeQueue&amp;lt;T&amp;gt;();
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; FireEvent(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;ref&lt;/span&gt; EventHandler targetEvent)
        {
            Eventing.FireEvent(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;ref&lt;/span&gt; targetEvent, &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;);
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Dispose(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; disposing)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (disposing)
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Queue.Clear();
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Dispose()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Dispose(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;);
        }
    }&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;I know I am tearing through this quickly, but it&amp;#39;s a lot to explain.&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Now we need to have some sort of managed work unit that the thread can work on.&amp;nbsp; We want it&amp;#39;s work unit to be generic and should be easily derived from:&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;abstract&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; WorkerBee
    {
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; WorkerBee() { }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;abstract&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Start();
    }
 
    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;abstract&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; WorkerBee&amp;lt;T&amp;gt; : WorkerBee
    {
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; T WorkUnit { get; set; }
    }
 
    &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This worker can be used as it.  Subscribe to the WorkBegan and WorkFinished events to inject logic.&lt;/span&gt;
    &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;
    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; AnyWorkerBee&amp;lt;T&amp;gt; : WorkerBee&amp;lt;T&amp;gt;
    {
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;delegate&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; WorkerEventHandler(WorkerBee&amp;lt;T&amp;gt; sender);
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;event&lt;/span&gt; WorkerEventHandler WorkBegan;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;event&lt;/span&gt; WorkerEventHandler WorkFinished;
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;virtual&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; OnWorkBegan()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkBegan !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkBegan(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;);
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;virtual&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; OnWorkFinished()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkFinished !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkFinished(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;);
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;virtual&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; WorkStart()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.OnWorkBegan();
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;protected&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;virtual&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; WorkFinish()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.OnWorkBegan();
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;virtual&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; DoWork() { }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;override&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Start()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkStart();
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DoWork();
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkFinish();
        }
    }&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;Now we can look at the actual dispatcher, we need some way to add work to a queue, have it spawn a thread if it has any left, and then execute the work.&amp;nbsp; One of the tricky things was to make sure the dispatcher thread was not ALWAYS running.&amp;nbsp; It would start itself up anytime work was added, continue running and waiting until it&amp;#39;s work queue was empty, then shut down.&amp;nbsp; Every subsequent addition to the queue would ensure that the dispatcher was still running.&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt; 
&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;class&lt;/span&gt; Dispatcher : IDisposable
    {
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; Dispatcher(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; maxThreadCount)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ResourcePool&amp;lt;Thread&amp;gt;(
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ResourcePool&amp;lt;Thread&amp;gt;.CreateResourceDelegate(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.CreateThread),
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ResourcePool&amp;lt;Thread&amp;gt;.CanReuseResourceDelegate(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.ValidateThread));
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.MaxRetry &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; 0;
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ThreadSafeQueue&amp;lt;WorkerBee&amp;gt;();
 
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.MaxResourceCount &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; maxThreadCount;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.EnsureDispatch(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;); &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;// setup the dispatcher&lt;/span&gt;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Call this to stop the dispatcher&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Abort()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.AbortDispatch &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Call this to pause the dispatcher&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Pause()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.AbortDispatch &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Call this to resume the dispatcher&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Resume()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.AbortDispatch)
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.AbortDispatch &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;;
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.EnsureDispatch();
            }
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Set this to prevent the dispatcher from spawning threads.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; AbortDispatch
        {
            get;
            set;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the thread that will dispatch work to other threads.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; Thread DispatchThread
        {
            get;
            set;
        }&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;object&lt;/span&gt; _DispatcherLock &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Object();
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This will make sure that the Dispatcher is active and running.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; EnsureDispatch()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.EnsureDispatch(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;);
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This will make sure that the Dispatcher is active and running.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; EnsureDispatch(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; start)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;lock&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;._DispatcherLock)
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt; || &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread.ThreadState == ThreadState.Stopped)
                {
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Thread(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ThreadStart(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.FireThread));
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread.IsBackground &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;;
                }
 
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (start &amp;amp;&amp;amp; ((&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread.ThreadState &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;|&lt;/span&gt; ThreadState.Unstarted) == &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread.ThreadState))
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchThread.Start();
            }
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is a required callback for the Resource pool.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;param name=&amp;quot;obj&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; ValidateThread(Thread obj)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;false&lt;/span&gt;; &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//never reuse threads&lt;/span&gt;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the method that the resource pool will call to get a new thread to work with.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; Thread CreateThread()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; Thread(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;new&lt;/span&gt; ParameterizedThreadStart(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.DispatchWork));
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// The function starts the work on a new thread.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; DispatchWork(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;object&lt;/span&gt; state)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;try&lt;/span&gt;
            {
                WorkerBee work &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; state &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;as&lt;/span&gt; WorkerBee;
 
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (work !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                    work.Start();
            }
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;//Give the thread back.&lt;/span&gt;
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.Release(Thread.CurrentThread);
            }
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This puts work on the queue and starts the dispatcher&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;param name=&amp;quot;obj&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; AddToQueue(WorkerBee obj)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue.Enqueue(obj);
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.EnsureDispatch();
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;int&lt;/span&gt; GetQueueLength()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;return&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue.Count;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the callback that the dispatcher thread calls.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// It will initialize a new thread from the pool and start work&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; FireThread()
        {
            WorkerBee work &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;;
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;while&lt;/span&gt; (!AbortDispatch &amp;amp;&amp;amp; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue.TryDequeue(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;out&lt;/span&gt; work))
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (work !&lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;null&lt;/span&gt;)
                {
                    Thread thread &lt;span style="color:Red;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;=&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.Get();
                    
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (!AbortDispatch) &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;// check again&lt;/span&gt;
                        thread.Start(work);
                    &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;else&lt;/span&gt;
                        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.Release(Thread.CurrentThread);
                }
            }
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// This is the queue of stuff to be done.&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; ThreadSafeQueue&amp;lt;WorkerBee&amp;gt; WorkQueue
        {
            get;
            set;
        }
 
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// Simple resource pool filled with threads&lt;/span&gt;
        &lt;span style="color:Green;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;private&lt;/span&gt; ResourcePool&amp;lt;Thread&amp;gt; Threads
        {
            get;
            set;
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Dispose(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;bool&lt;/span&gt; disposing)
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;if&lt;/span&gt; (disposing)
            {
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.WorkQueue.Clear();
                &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Threads.Dispose();
            }
        }
 
        &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;public&lt;/span&gt; &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;void&lt;/span&gt; Dispose()
        {
            &lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;this&lt;/span&gt;.Dispose(&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;true&lt;/span&gt;);
        }
    }&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://devplanet.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/brianr.Code+Snippets/ThreadingDemo.zip"&gt;ThreadingDemo.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Download the code and test project and try it for yourself.&amp;nbsp; I will say that this is fairly experimental, although I have been using it for quite a few things and have experienced no issues.&amp;nbsp; One of the major things I have used it for is the cache scavenger in my Factory caching model and it seems to work great.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=13616" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/brianr/archive/tags/Threading/default.aspx">Threading</category><category domain="http://devplanet.com/blogs/brianr/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Silverlight 2 XAML Binding and Custom / Core Dependency Properties</title><link>http://devplanet.com/blogs/justice/archive/2008/11/23/silverlight-2-xaml-binding-and-custom-core-dependency-properties.aspx</link><pubDate>Sun, 23 Nov 2008 18:14:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:13468</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The basic scenario is as follows: &amp;nbsp;I have done quite a bit of WPF programming in the past (since back when it was WinFx). &amp;nbsp;I feel pretty damn good about the bindings in WPF, and I feel like I have wrapped my head around binding and dependency properties pretty well. &amp;nbsp;So, I thought when I decided to write Silverlight 2 apps rather than straight to the vein, white horse, WPF apps, it would be a very similar feeling. &amp;nbsp;The feeling was abruptly interrupted by the evilness of the ElementName Binding param. &amp;nbsp;For those of you who have spent time writing WPF apps, you will know the joy of binding element properties to each other and having no problems. &amp;nbsp;It&amp;#39;s easy to take such things for granted.&lt;/p&gt;...(&lt;a href="http://devplanet.com/blogs/justice/archive/2008/11/23/silverlight-2-xaml-binding-and-custom-core-dependency-properties.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devplanet.com/aggbug.aspx?PostID=13468" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/justice/archive/tags/Silverlight+2/default.aspx">Silverlight 2</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Binding/default.aspx">Binding</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Dependency+Property/default.aspx">Dependency Property</category></item><item><title>Silver Lining for Windows Azure -- Silverlight 2 Sample Hosted in Azure</title><link>http://devplanet.com/blogs/justice/archive/2008/11/08/silver-lining-for-windows-azure-silverlight-2-sample-hosted-in-azure.aspx</link><pubDate>Sat, 08 Nov 2008 16:00:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12331</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I melded an Azure Web Role project and the Silverlight Toolkit sample to give you guys a quick download, so you can play with them together.&amp;nbsp; I hope you like it, and I hope it helps you explore some of the new technology.&amp;nbsp; I call it&amp;nbsp;&lt;a target="_blank" title="SiverLining.zip" href="/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/justice/SilverLining.zip"&gt;SilverLining&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;...(&lt;a href="http://devplanet.com/blogs/justice/archive/2008/11/08/silver-lining-for-windows-azure-silverlight-2-sample-hosted-in-azure.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12331" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/justice/archive/tags/Azure/default.aspx">Azure</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Silverlight+Toolkit/default.aspx">Silverlight Toolkit</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Silverlight+2/default.aspx">Silverlight 2</category></item><item><title>Unique Identifier Flat File Import with SSIS</title><link>http://devplanet.com/blogs/justice/archive/2008/11/07/unique-identifier-flat-file-import-with-ssis.aspx</link><pubDate>Fri, 07 Nov 2008 16:44:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12307</guid><dc:creator>David Justice</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This morning I found myself wrestling with SSIS to convert a string from a flat file to a unique identifier via a Data Flow inside of an SSIS package. &amp;nbsp;I figured this would be a rather trivial case&amp;nbsp;(the midget was kicking my ass!). &amp;nbsp;I use guids all over for application development and for database keys. &amp;nbsp;They are very useful, and very dependable. &amp;nbsp;Here is a line from my flat file:&lt;/p&gt;
&lt;p&gt;61898d4c-3a1b-4736-9a36-eb90a23322e0|:|10/13/2008 11:59:58 PM|:|74afebfe-9523-4977-b5f6-5154b6c91211&lt;/p&gt;
&lt;p&gt;Anyone see anything wrong with this? &amp;nbsp;I didn&amp;#39;t see anything wrong, outside of the silly |:| delimiter (I don&amp;#39;t want to get started on that &amp;lt;rant/&amp;gt; &amp;lt;--- rant with no content!).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;...(&lt;a href="http://devplanet.com/blogs/justice/archive/2008/11/07/unique-identifier-flat-file-import-with-ssis.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12307" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/justice/archive/tags/GUID/default.aspx">GUID</category><category domain="http://devplanet.com/blogs/justice/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devplanet.com/blogs/justice/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Unique+Identfier/default.aspx">Unique Identfier</category></item><item><title>MetaTable Expression Builder for Fetching Any Object Via Collection of Primary Keys</title><link>http://devplanet.com/blogs/justice/archive/2008/11/04/metatable-expression-builder-for-fetching-any-object-via-collection-of-primary-keys.aspx</link><pubDate>Tue, 04 Nov 2008 14:53:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12199</guid><dc:creator>David Justice</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Basically, I wanted to build a method that gets an object from a data context whatever the primary key may look like, and whatever type that object may be. &amp;nbsp;I wrote this code a little while ago when I was exploring the DynamicData libs. &amp;nbsp;I was checking out some of the code in the libs, and noticed a very slick way one of devs was building expressions to grab back a data item based on a single key ID. &amp;nbsp;I thought the idea of building up the expression completely ROCKED, but I wanted to expand it out to include composite primary keys. &amp;nbsp;Below is my implementation of the composite primary key object fetcher. &amp;nbsp;Hopefully, it will make your life a little easier.&lt;/p&gt;...(&lt;a href="http://devplanet.com/blogs/justice/archive/2008/11/04/metatable-expression-builder-for-fetching-any-object-via-collection-of-primary-keys.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12199" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/justice/archive/tags/MetaModel/default.aspx">MetaModel</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Expressions/default.aspx">Expressions</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Expression+Building/default.aspx">Expression Building</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devplanet.com/blogs/justice/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>Windows Azure and _NOT_ SQLExpress</title><link>http://devplanet.com/blogs/justice/archive/2008/11/02/windows-azure-and-not-sqlexpress.aspx</link><pubDate>Sun, 02 Nov 2008 17:37:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12196</guid><dc:creator>David Justice</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I&amp;#39;ve been very excited recently by the release of Windows Azure, and rest of the Microsoft suite of cloud based services. &amp;nbsp;I downloaded all of the sdk&amp;#39;s, signed up for all the invitation tokens I could, and began writing code. &amp;nbsp;The Visual Studio templates for Windows Azure projects makes getting your first Azure project up and running super simple. &amp;nbsp;I did run into one issues with the project templates setting up my first project.&lt;/p&gt;...(&lt;a href="http://devplanet.com/blogs/justice/archive/2008/11/02/windows-azure-and-not-sqlexpress.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12196" width="1" height="1"&gt;</description><category domain="http://devplanet.com/blogs/justice/archive/tags/Azure/default.aspx">Azure</category><category domain="http://devplanet.com/blogs/justice/archive/tags/SQLExpress/default.aspx">SQLExpress</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://devplanet.com/blogs/justice/archive/tags/Cloud+Computing/default.aspx">Cloud Computing</category></item><item><title>Work Harder</title><link>http://devplanet.com/blogs/brianr/archive/2008/10/21/work-harder.aspx</link><pubDate>Tue, 21 Oct 2008 15:26:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12160</guid><dc:creator>Brian Rudolph</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;Work Harder!&amp;nbsp; That is a statement I find myself repeating daily.&amp;nbsp; People often wonder what makes one person better at a certain task than others.&amp;nbsp; There is a truly simple answer, practice.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Natural ability is a myth. &amp;nbsp;With the exception of physical limitations, all ability is gained through practice.&amp;nbsp; No infant is born with the innate ability to walk.&amp;nbsp; No musician picks up an instrument for the first time and performs a flawless number.&amp;nbsp; Everything &amp;lsquo;great&amp;#39; is preceded by countless not-so-great attempts.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;The human race is a term that to some is a simple definition of our status as homosapiens.&amp;nbsp; To me, it is a bit more literal.&amp;nbsp; I recognize that there are billions of people in this world, all of whom want the same things I do.&amp;nbsp; What sets certain individuals apart from the pack is their willingness to work harder than the rest.&amp;nbsp; So for me, it&amp;#39;s the race of humans.&amp;nbsp; Who wants to work harder?&amp;nbsp; Who wants to spend that extra effort day after day to be the best at what they do.&amp;nbsp; Someone who doesn&amp;#39;t work as hard as I do does not deserve the same things I deserve, just as I do not deserve the same benefits of the people who work harder than I am willing to.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I hear complaints from people about the state of their lives, every argument of which never focuses on their lack of effort.&amp;nbsp; It is always the result of some innocuous outside influence.&amp;nbsp; Bunk.&amp;nbsp; People who work hard, achieve more.&amp;nbsp; Good things happen to good people who work hard.&amp;nbsp; Being good isn&amp;#39;t enough.&amp;nbsp; There are lots of &amp;quot;good&amp;quot; people, if the definition of &amp;quot;good&amp;quot; can be simplified to &amp;quot;not being bad&amp;quot;.&amp;nbsp; Spend a few more hours with your face buried in a book rather than a pint of beer.&amp;nbsp; Unless, of course, your goal is to be the best drinker on earth.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Everything that has gone right in my life happened to me when I went the extra mile.&amp;nbsp; Every real accomplishment didn&amp;#39;t come from the 8 to 5 daily grind.&amp;nbsp; Good things happened during the hundred-hour weeks of work.&amp;nbsp; Times when 8 to 5 wouldn&amp;#39;t have been enough to get the job done, allowing the opportunity to pass right by.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;There have been many times I have explained to people that epiphanies have come to me in my sleep.&amp;nbsp; Though I was not lying, this is not a result of me having some abnormal super power.&amp;nbsp; It is a result of me wrapping my mind so deeply around a problem that I am consumed by it even during my few hours of sleep.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I like to think that everything that has happened to me in my life, I&amp;#39;ve earned.&amp;nbsp; This, of course, includes all of the bad things.&amp;nbsp; For many years my interests were simply in having fun, so my career took a nose-dive.&amp;nbsp; Then I focused entirely on work which destroyed countless relationships, as well as my physical health. So while balance has become important, I have recognized that&amp;nbsp;every decision&amp;nbsp;comes with consequences;&amp;nbsp;consequences which I hope to never complain about.&lt;/p&gt;
&lt;p&gt;People make choices in their lives.&amp;nbsp; If your choice is to pursue a career or path in your life that will never financially stabilize, you have no right to complain about finances.&amp;nbsp; You have chosen to sacrifice financial success for your love or interest in what it is you do.&amp;nbsp; It is not society&amp;#39;s responsibility to fill in for your financial woes.&amp;nbsp; If you choose to sacrifice your personal life for professional glory, then do not cry about your lack of intimacy.&amp;nbsp; You chose that path.&amp;nbsp; Just realize that everything requires hard work.&amp;nbsp; Even if an individual&amp;#39;s goal in life is to be the best parent their child could have, then they must work for it.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;This is not to say that I am better or worse than anyone else in this world.&amp;nbsp; What I am attempting to say is that I take responsibility for what happens in my life.&amp;nbsp; I will not give up, I will not rest until I am the best I can be at what I do, regardless of what this life throws at me.&amp;nbsp; The best I can be at anything will require a lifetime of effort.&amp;nbsp; So I suppose it is a valid assumption to say that I will only achieve this in the moment immediately before my death.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I truly wish more people would take responsibility for their lives.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devplanet.com/aggbug.aspx?PostID=12160" width="1" height="1"&gt;</description></item><item><title>Thread Safe Dictionary Update</title><link>http://devplanet.com/blogs/brianr/archive/2008/09/29/thread-safe-dictionary-update.aspx</link><pubDate>Mon, 29 Sep 2008 16:14:00 GMT</pubDate><guid isPermaLink="false">6e120f4b-f509-4111-8fd8-03bc0d0a75d9:12123</guid><dc:creator>Brian Rudolph</dc:creator><slash:comments>6</slash:comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For those of you that have read my post about the Thread Safe Dictionary, I have a few updates.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;In recent months, I have done a ridiculous amount of multi-threaded work.&amp;nbsp; This has forced me to expand my threading libraries.&amp;nbsp; One of the primary objects that required changes\fixes was my dictionary.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The dictionary uses ReaderWriterLockSlim locks.&amp;nbsp; These are clean, lightweight locks that MS has provided.&amp;nbsp; Most importantly, they allow upgradeable read locks.&amp;nbsp; I now use simple IDisposable locking wrappers.&amp;nbsp; This allows us to use the &amp;quot;using&amp;quot; syntax to ensure lock releases.&lt;/p&gt;
&lt;p&gt;It is important to note that no safe dictionary can provide safe inserts without the caller forcing a lock.&amp;nbsp; This is because by the time you check for the existence of an object and then insert it, it may have been inserted by another thread.&amp;nbsp; The only way to prevent this is by doing a &amp;quot;blind&amp;quot; merge.&amp;nbsp; The reason&amp;nbsp;I call it blind, is that when you do a merge, we check for existence, and do a replace\insert within the scope of a single write lock.&amp;nbsp; This means you cannot control which objects get replaced in the dictionary.&amp;nbsp; This is rarely important in caching situations.&lt;/p&gt;
&lt;p&gt;&lt;table cellpadding="0" cellspacing="0" style="background-color:#f2f2f2;border:solid 1px #e5e5e5;width:100%;"&gt;
&lt;tr style="vertical-align:top;line-height:normal;"&gt;&lt;td&gt;&lt;pre style="overflow:scroll;margin:0px;padding:2px;padding-left:8px;"&gt;&lt;span style="color:Black;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; System;
&lt;span style="color:Blue;background-color:Transparent;font-family:Courier New;font-size:11px;font-weight:normal;"&gt;using&lt;/span&gt; System.Collections;
&lt;span style="color:Blue;