<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Ozgur Ozcitak</title>
 <link href="http://ozgur.ozcitak.com/atom.xml" rel="self"/>
 <link href="http://ozgur.ozcitak.com/"/>
 <updated>2012-01-16T03:18:52-08:00</updated>
 <id>http://ozgur.ozcitak.com/</id>
 <author>
   <name>Ozgur Ozcitak</name>
   <email>ozgur@ozcitak.com</email>
 </author>

 
 <entry>
   <title>Keep Your Async Calls Organized with Step</title>
   <link href="http://ozgur.ozcitak.com/blog/2010/12/23/keep-your-async-calls-organized-with-step.html"/>
   <updated>2010-12-23T00:00:00-08:00</updated>
   <id>http://ozgur.ozcitak.com/blog/2010/12/23/keep-your-async-calls-organized-with-step</id>
   <content type="html">&lt;h1&gt;Keep Your Async Calls Organized with Step&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;23 Dec 2010&lt;/p&gt;
&lt;p&gt;You read all about &lt;a href=&quot;http://nodejs.org/&quot;&gt;node.js&lt;/a&gt;, went through the canonical hello world samples and started using node.js with a real application. Sooner or later your code will turn into an async callback jungle: you will have callbacks nested many levels deep and blocks of code with complex logic checks to determine when certain async calls are completed. When you find yourself facing this problem, know that there is a cure; and it comes as a node.js module called &lt;code&gt;step&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;step&lt;/code&gt; is written by Tim Caswell of &lt;a href=&quot;http://howtonode.org/&quot;&gt;How To Node&lt;/a&gt;. You can clone the &lt;a href=&quot;https://github.com/creationix/step/&quot;&gt;github repository&lt;/a&gt; or install &lt;code&gt;step&lt;/code&gt; with &lt;code&gt;npm install step&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;step&lt;/code&gt; lets you organize your async function calls in serial order, or process them in parallel. Serial processing in &lt;code&gt;step&lt;/code&gt; looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readdir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__dirname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;dirs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This function will be chained with readdir above&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This could also be achieved without using &lt;code&gt;step&lt;/code&gt; by nesting async callbacks, but this tends to get unreadable after a couple levels.&lt;/p&gt;
&lt;p&gt;Parallel processing in &lt;code&gt;step&lt;/code&gt; looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT * FROM posts LIMIT 20&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parallel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT COUNT(*) FROM posts&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;parallel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;countRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This function will be called after both async calls above&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// are completed&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With parallel processing, &lt;code&gt;step&lt;/code&gt; will call the next function after all async calls from the previous step are completed. Parameters of each async callback will be passed to the next step function as separate arguments. There is also another variant of parallel processing called grouping:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT authorid FROM posts&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;authorids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;authorids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT name FROM authors WHERE id=?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;authornames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This function will be called after all async calls above&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// are completed&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The difference between &lt;code&gt;parallel&lt;/code&gt; and &lt;code&gt;group&lt;/code&gt; is that when grouping; the results of all async callbacks will be stuffed in one result array, whereas with parallel processing, results of each async callback are kept separate.&lt;/p&gt;
&lt;p&gt;There is also one caveat of grouping. The next step function will never be called if the group is empty; that is if there are no async callbacks from the previous step. This is probably by design but there may be times when this is not desirable. In that case, you need to add a check yourself:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;nx&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT authorid FROM posts&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;authorids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Make sure we reach the next step&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;authorids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[];&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;authorids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;SELECT name FROM authors WHERE id=?&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;authornames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// This function will be called after either all async calls above&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// are completed -OR- there were no async calls&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;UPDATE&lt;/span&gt;:&lt;/strong&gt; This issue is now fixed in the latest release of &lt;code&gt;step&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;step&lt;/code&gt; is a simple function but it helps simplify your code a lot. Use it.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Using GDI Objects the Right Way</title>
   <link href="http://ozgur.ozcitak.com/blog/2009/12/23/using-gdi-objects-the-right-way.html"/>
   <updated>2009-12-23T00:00:00-08:00</updated>
   <id>http://ozgur.ozcitak.com/blog/2009/12/23/using-gdi-objects-the-right-way</id>
   <content type="html">&lt;h1&gt;Using &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; Objects the Right Way&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;23 Dec 2009&lt;/p&gt;
&lt;p&gt;Since I am writing some graphics-heavy user controls lately, I am spending a lot of time googling for &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; related topics. Looking through the samples/demonstrations it appears that there is some confusion about how and when to dispose &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects. It is common to see rendering routines where brushes/pens/fonts are created without being disposed, or &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects created in the control&amp;#8217;s constructor, released (if at all) in the control&amp;#8217;s Dispose method. I will try to address these issues in the form of a &lt;span class=&quot;caps&quot;&gt;FAQ&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why should &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects be disposed?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There is a &lt;a href=&quot;http://support.microsoft.com/kb/894500&quot;&gt;hard limit&lt;/a&gt; for the total number of &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects available on a Windows system. For the Windows 9x family (Windows 95, 98 and ME) this limit is 1200 objects, for the NT family (Windows XP, Vista and Windows 7) the limit is 10,000. Although the number seems high, this is the total number of objects available across the system (&lt;em&gt;Edit: For the NT family the limit is per process&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Whenever you create a new &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; object, you are pulling a resource out of the shared &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; resource pool, effectively decreasing the resources available to other running applications and the rest of the system. The &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; object you created cannot be used by other applications because &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx&quot;&gt;&lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; handles are private&lt;/a&gt; to the process that created the object.&lt;/p&gt;
&lt;p&gt;When &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects are disposed, their associated handles will be freed and returned to the resource pool, to be reused when needed. This is why all &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects implement the &lt;code&gt;IDisposable&lt;/code&gt; interface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why should I explicitly dispose &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After all, the garbage collector will happily clean after us, right? The answer to this question is a lot longer that one would expect. I am going to give you the short version but I encourage you to &lt;a href=&quot;http://vineetgupta.spaces.live.com/blog/cns!8DE4BDC896BEE1AD!1104.entry&quot;&gt;read&lt;/a&gt; &lt;a href=&quot;http://www.informit.com/articles/article.aspx?p=99977&amp;amp;seqNum=7&quot;&gt;more&lt;/a&gt; &lt;a href=&quot;http://www.csharphelp.com/2006/08/garbage-collection/&quot;&gt;about&lt;/a&gt; &lt;a href=&quot;http://msdn.microsoft.com/en-us/magazine/bb985010.aspx&quot;&gt;the &lt;span class=&quot;caps&quot;&gt;CLR&lt;/span&gt; garbage collector&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What makes &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects special is that they encapsulate unmanaged handles which cannot be simply freed by the garbage collector. A managed &lt;code&gt;System.Drawing.Brush&lt;/code&gt; object for example keeps a handle to an unmanaged brush object. When it is disposed, its &lt;code&gt;Dispose&lt;/code&gt; method calls the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms533974(VS.85).aspx&quot;&gt;GdipDeleteBrush&lt;/a&gt; function to clean up resources used by the unmanaged brush handle. But what happens when you do not explicitly call &lt;code&gt;Dispose&lt;/code&gt;? In that case, sometime after the object goes out of scope, its finalizer will kick in and call the &lt;code&gt;Dispose&lt;/code&gt; method for you. If you look at the finalizer of &lt;code&gt;System.Drawing.Brush&lt;/code&gt; with &lt;a href=&quot;http://www.red-gate.com/products/reflector/&quot;&gt;reflector&lt;/a&gt;, you will see this one line:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Brush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dispose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Why the hassle then? The framework designers made sure that unmanaged resources will be cleaned up no matter how sloppy we are. The reason is, there are two problems with finalizers. First, the exact time an object&amp;#8217;s finalizer is executed is undefined. Actually, it is not guaranteed to be executed at all! Second, finalizers are expensive. It will require at least two garbage collections to free a finalizable object. Explicitly calling dispose overcomes both of these problems: unmanaged resources will be cleaned up immediately and the object will be exempt from finalization.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When should I dispose &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The short answer is &amp;#8220;as soon as possible&amp;#8221;. For most &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects (pens, brushes and even fonts), the overhead of creating and disposing the object is almost non-existent. I ran a quick benchmark while writing this and here are the results:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;Created 10,000 fonts in 61 milliseconds.
Created 10,000 brushes in 17 milliseconds.
Created 10,000 pens in 16 milliseconds.
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If your control uses 20 fonts, brushes and pens each, you will need a total of 0.2 milliseconds to create and dispose those objects. It doesn&amp;#8217;t really make sense keeping references to such objects; it is best to create, use and dispose them on the spot.&lt;/p&gt;
&lt;p&gt;For some graphics objects this may not be the case. Creating an &lt;code&gt;Image&lt;/code&gt; for example, may take anywhere from a few milliseconds to many seconds. If you are continuously rendering the same large image, you may be better off holding a reference to it to avoid the overhead. Just remember to dispose your image when you no longer need it.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How should I dispose &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; objects?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;C#&amp;#8217;s &lt;code&gt;using&lt;/code&gt; keyword was made for this purpose. It provides the additional benefit of calling &lt;code&gt;Dispose&lt;/code&gt; even when an exception is thrown inside the &lt;code&gt;using&lt;/code&gt; block. Its usage is as simple as this:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Brush&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brush&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SolidBrush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Black&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;graphics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FillRectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It also has an additional form that can be used when creating multiple objects:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;font&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Arial&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10.0f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Brush&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brush&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SolidBrush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Black&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;graphics&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DrawString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;some text&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There is another less useful form of the &lt;code&gt;using&lt;/code&gt; keyword, limited to objects of the same type:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;headingFont&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Times New Roman&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;12.0f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;bodyFont&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Arial&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10.0f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And a side note&amp;#8230; It is perfectly fine to return from a function inside the &lt;code&gt;using&lt;/code&gt; block. Your objects will be disposed just before the return statement is evaluated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Summing It Up&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Whenever you are creating a &lt;span class=&quot;caps&quot;&gt;GDI&lt;/span&gt; resource (actually any object implementing &lt;code&gt;IDisposable&lt;/code&gt;), enclose it in a &lt;code&gt;using&lt;/code&gt; block. You can make an exception if you are sure that object creation is expensive. In that case, keep a reference to the object and explicitly dispose it at the earliest opportunity.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Control.Visible May Not Return What You Expect</title>
   <link href="http://ozgur.ozcitak.com/blog/2009/11/16/control-visible-may-not-return-what-you-expect.html"/>
   <updated>2009-11-16T00:00:00-08:00</updated>
   <id>http://ozgur.ozcitak.com/blog/2009/11/16/control-visible-may-not-return-what-you-expect</id>
   <content type="html">&lt;h1&gt;Control.Visible May Not Return What You Expect&lt;/h1&gt;
&lt;p class=&quot;meta&quot;&gt;16 Nov 2009&lt;/p&gt;
&lt;p&gt;I encountered a curious issue while working on &lt;a href=&quot;http://code.google.com/p/imagelistview/&quot;&gt;ImageListView&lt;/a&gt;. In the layout code of the control I have a check to determine whether to show the vertical scrollbar. If the scrollbar needs to be shown, the layout is recalculated since the display area will be smaller with the scrollbar shown. However, my layout code ended up being called infinitely resulting in a stack overflow eventually. Here is the relevant part of the layout code.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UpdateLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Maximum number of rows and columns that can be fully displayed&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;displayBounds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Width&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itemSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;displayBounds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Height&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;itemSize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Height&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// Check if we need the vertical scroll bar&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vScrollRequired&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cols&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vScrollBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Visible&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vScrollRequired&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// Show the scrollbar&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;vScrollBar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Visible&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vScrollRequired&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;c1&quot;&gt;// Recalculate the layout&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;UpdateLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The problem was that the Visible property of the scrollbar always returned false, if the layout code was called before the parent control is displayed. This is because &amp;#8211; as I learned the hard way -, the getter of the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visible.aspx&quot;&gt;Control.Visible&lt;/a&gt; property checks whether its parent control is visible. If not, it will return false regardless of what you set.&lt;/p&gt;
&lt;p&gt;The solution was simple though. Keep a separate variable to track scrollbar&amp;#8217;s visibility rather than relying on Control.Visible.&lt;/p&gt;
&lt;p&gt;Here is what I do not get. If the Visible property actually means &amp;#8220;the control is displayed on the screen&amp;#8221; then why does it return true when the control is hidden under another control, or the control is moved beyond the client area of its parent?&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.aspx&quot;&gt;ToolStripItem&lt;/a&gt; circumvents the issue by introducing the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripitem.available.aspx&quot;&gt;Available&lt;/a&gt; property. In the remarks section &lt;span class=&quot;caps&quot;&gt;MSDN&lt;/span&gt; says:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The Available property is different from the Visible property in that Available indicates whether the ToolStripItem is shown, while Visible indicates whether the ToolStripItem and its parent are shown. Setting either Available or Visible to true or false sets the other property to true or false.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is what Control.Visible should be doing in the first place. To determine if the control is actually displayed on the screen, there should have been another read-only property (IsDisplayed perhaps?).&lt;/p&gt;
&lt;p&gt;It may not be possible to fix Control.Visible since there are probably many applications relying on the current behavior. However, it should be possible to extend ToolStripItem.Available property to the Control class without causing many compatibility issues.&lt;/p&gt;</content>
 </entry>
 
 
</feed>