<p>X Windows has special processes called Window Managers that manage the layout of windows and decorations like their title bar, control buttons etc. Such processes use an X Windows API to detect events related to windows sizes and positions.</p>

<p>Are there any consistent ways for writing such processes for Microsoft Windows or Mac OS/X?</p>

<p>I know that in general these systems are less flexible but I'm looking for something that will use public APIs and not undocumented hacks.</p>

Garbage collection will gather up the first new when it is run.
Only the second one you purposely dispose in the Finally block.  The first one will be disposed of the next time the garbage collection is run.
I do not think this is a good idea.  If the first command is not closed correctly it is possible you would have an open connection to the database and it will not be disposed.
A better way would be to dispose the first command after you are done using it, and then to reuse it.