<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Michael Entin's notebook : .NET</title><link>http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Random() is only random if you are using it right</title><link>http://blogs.msdn.com/michen/archive/2008/02/05/Random_28002900_-is-only-random-if-you-are-using-it-right.aspx</link><pubDate>Tue, 05 Feb 2008 11:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7460700</guid><dc:creator>michen</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/michen/comments/7460700.aspx</comments><wfw:commentRss>http://blogs.msdn.com/michen/commentrss.aspx?PostID=7460700</wfw:commentRss><wfw:comment>http://blogs.msdn.com/michen/rsscomments.aspx?PostID=7460700</wfw:comment><description>&lt;P&gt;I like the quote "With great power comes great responsibility" when used in regards to .NET - .NET gives one great powers, but use it wisely and know how this stuff works. &lt;/P&gt;
&lt;P&gt;Recently I saw code (it was written by a guy interviewing to our team) that demonstrated interesting problem with incorrect usage of Random class. The code generated a random position for an object, tested if it satisfied some condition; if it did not work - generated another random position, tested the new position and so on until it generated something that did work. The code to generate each position was pretty simple: create new instance of Random class, use it to generate 4 random integers. The percentage of failed position was not very big (but noticeable), the code to test the position was rather fast, and&amp;nbsp;we needed just 10 good positions to create complete configuration (all this was about a&amp;nbsp;variant of &lt;A class="" title="Battleship game on Wikipedia" href="http://en.wikipedia.org/wiki/Battleship_game" target=_blank mce_href="http://en.wikipedia.org/wiki/Battleship_game"&gt;Battleship game&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;But it took very long to generate the whole configuration - about a second on average.&lt;/P&gt;
&lt;P&gt;Playing with this code, I suddenly found that moving Random object to a member variable, so it is re-used, rather than creating new Random object&amp;nbsp;for each position, made the code 10000 times faster! Well, obviously I expected some savings since&amp;nbsp;it creates less objects, but ten thousand times? My first reaction - is the Random really such a heavy object?&lt;/P&gt;
&lt;P&gt;It turned out the problem was more interesting, and caused by the usage of default constructor. As MSDN puts it, if you use default constructor the "seed is initialized to a value based on the current time."&amp;nbsp;It actually uses GetTickCount() as the seed. Can you now spot the problem? The Random() constructor was very quick, but during the period of time when GetTickCount()&amp;nbsp;returns the same value (which is about 20ms for most chipsets),&amp;nbsp;all the Random&amp;nbsp;objects created this way&amp;nbsp;will have the same seed and generate the same &lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'; mso-ansi-language: EN-US; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;positions&lt;/SPAN&gt;! Instead of&amp;nbsp;trying new positions, the code tried the same position again and again until the GetTickCount() returned a new value. So don't generate too many Random instances - just one will behave much better in most cases.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7460700" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Functional sort in C#</title><link>http://blogs.msdn.com/michen/archive/2007/12/11/functional-sort-in-c.aspx</link><pubDate>Tue, 11 Dec 2007 22:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6738041</guid><dc:creator>michen</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/michen/comments/6738041.aspx</comments><wfw:commentRss>http://blogs.msdn.com/michen/commentrss.aspx?PostID=6738041</wfw:commentRss><wfw:comment>http://blogs.msdn.com/michen/rsscomments.aspx?PostID=6738041</wfw:comment><description>&lt;P&gt;On an internal mailing list, we were discussing functional languages, and this Haskell sort code:&lt;/P&gt;
&lt;P&gt;&lt;SPAN lang=EN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN"&gt;qsort []&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = []&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN lang=EN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN"&gt;qsort (x:xs) = qsort (&lt;A href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"&gt;&lt;SPAN style="BORDER-RIGHT: windowtext 1pt; PADDING-RIGHT: 0in; BORDER-TOP: windowtext 1pt; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt; COLOR: #006600; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt; TEXT-DECORATION: none; text-underline: none; mso-border-alt: none windowtext 0in"&gt;filter&lt;/SPAN&gt;&lt;/A&gt; (&lt;/SPAN&gt;&lt;SPAN lang=EN style="FONT-SIZE: 10pt; COLOR: #006600; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN lang=EN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-ansi-language: EN"&gt; x) xs) &lt;A href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:."&gt;&lt;SPAN style="BORDER-RIGHT: windowtext 1pt; PADDING-RIGHT: 0in; BORDER-TOP: windowtext 1pt; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt; COLOR: #006600; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt; TEXT-DECORATION: none; text-underline: none; mso-border-alt: none windowtext 0in"&gt;++&lt;/SPAN&gt;&lt;/A&gt; [x] &lt;A href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:."&gt;&lt;SPAN style="BORDER-RIGHT: windowtext 1pt; PADDING-RIGHT: 0in; BORDER-TOP: windowtext 1pt; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt; COLOR: #006600; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt; TEXT-DECORATION: none; text-underline: none; mso-border-alt: none windowtext 0in"&gt;++&lt;/SPAN&gt;&lt;/A&gt; qsort (&lt;A href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"&gt;&lt;SPAN style="BORDER-RIGHT: windowtext 1pt; PADDING-RIGHT: 0in; BORDER-TOP: windowtext 1pt; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt; COLOR: #006600; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt; TEXT-DECORATION: none; text-underline: none; mso-border-alt: none windowtext 0in"&gt;filter&lt;/SPAN&gt;&lt;/A&gt; (&lt;A href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:&amp;amp;gt;="&gt;&lt;SPAN style="BORDER-RIGHT: windowtext 1pt; PADDING-RIGHT: 0in; BORDER-TOP: windowtext 1pt; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt; COLOR: #006600; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt; TEXT-DECORATION: none; text-underline: none; mso-border-alt: none windowtext 0in"&gt;&amp;gt;=&lt;/SPAN&gt;&lt;/A&gt; x) xs)&lt;/SPAN&gt;&lt;SPAN lang=RU style="mso-ansi-language: RU"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;While trying to explain how this code works (which is very different from what it looks like&amp;nbsp;to C++/C# programmers due to &lt;A class="" href="http://en.wikipedia.org/wiki/Lazy_evaluation" target=_blank mce_href="http://en.wikipedia.org/wiki/Lazy_evaluation"&gt;lazy evaluation&lt;/A&gt;) I've come up with following C#&amp;nbsp;code (with Linq) that is logically similar to Haskell version. Obviously, Haskell code is much nicer though.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Consolas"&gt;static&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;T&amp;gt; Qsort&amp;lt;T&amp;gt;(&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;T&amp;gt; s) &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; T : &lt;SPAN style="COLOR: #2b91af"&gt;IComparable&lt;/SPAN&gt;&amp;lt;T&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerator&lt;/SPAN&gt;&amp;lt;T&amp;gt; e = s.GetEnumerator();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (e.MoveNext())&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T x = e.Current;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (T t &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; Qsort(s.Skip(1).Where(y =&amp;gt; y.CompareTo(x) &amp;lt; 0)))&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; t;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; x;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (T t &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; Qsort(s.Skip(1).Where(y =&amp;gt; y.CompareTo(x) &amp;gt;= 0)))&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;yield&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; t;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Consolas"&gt;&lt;FONT face=Arial&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6738041" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/michen/archive/tags/Functional/default.aspx">Functional</category></item><item><title>Don't run SSIS package using SQL/CLR</title><link>http://blogs.msdn.com/michen/archive/2007/08/24/don-t-run-ssis-package-using-sql-clr.aspx</link><pubDate>Sat, 25 Aug 2007 03:57:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4550316</guid><dc:creator>michen</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/michen/comments/4550316.aspx</comments><wfw:commentRss>http://blogs.msdn.com/michen/commentrss.aspx?PostID=4550316</wfw:commentRss><wfw:comment>http://blogs.msdn.com/michen/rsscomments.aspx?PostID=4550316</wfw:comment><description>&lt;P&gt;A&amp;nbsp;recent &lt;A class="" title=Comment href="http://blogs.msdn.com/michen/archive/2007/03/22/running-ssis-package-programmatically.aspx#4401800" mce_href="http://blogs.msdn.com/michen/archive/2007/03/22/running-ssis-package-programmatically.aspx#4401800"&gt;commenter&lt;/A&gt; suggested running SSIS using SQL/CLR:&lt;/P&gt;
&lt;BLOCKQUOTE class=Q&gt;
&lt;P&gt;Just an idea on how to do this that may be a bit easier than any of the methods covered. &amp;nbsp;IF you were to write a CLR procedure which accepts a string as its parameter. &amp;nbsp;The string passed in would be the xml definition of a package (either loaded from a source (file, server, etc)or from an application which is able to create the package definition). &amp;nbsp;Then using the CLR procecure that I mentioned above (which has references to the DTS runtime), you could use the Microsoft.SqlServer.Dts.Runtime.Package LoadFromXML() method to flesh out an empty package and then the Execute() method to actually run it. &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This may look like a good idea, but don't do this. The reason is that SQL Server has very&amp;nbsp;strict requirements on the type of code running inside (this is how it achieves the great reliability, and why by default it only allows "safe" .NET code and only selected system assemblies). It takes a lot of effort to "harden" code to satisfy these requirements. See e.g. this excellent article by Joe Duffy describing one of the issues: &lt;A href="http://www.bluebytesoftware.com/blog/2007/08/23/ThreadInterruptsAreAlmostAsEvilAsThreadAborts.aspx"&gt;http://www.bluebytesoftware.com/blog/2007/08/23/ThreadInterruptsAreAlmostAsEvilAsThreadAborts.aspx&lt;/A&gt;. Only a subset of .NET framework has been "hardened" to qualify.&lt;/P&gt;
&lt;P&gt;The SSIS uses "unsafe" .NET code, and we've not done enough testing of SSIS and dependencies inside SQL Server to recommend running it this way, so it is unsupported.&lt;/P&gt;
&lt;P&gt;You'd be safer if you write a web service application that does it, and the extra benefit - &lt;A class="" title="Running SSIS package via Web Service" href="http://msdn2.microsoft.com/en-us/library/ms403355.aspx#service" mce_href="http://msdn2.microsoft.com/en-us/library/ms403355.aspx#service"&gt;SQL Books Online has sample code&lt;/A&gt;!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=4550316" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/michen/archive/tags/SSIS/default.aspx">SSIS</category><category domain="http://blogs.msdn.com/michen/archive/tags/SSIS+Programming/default.aspx">SSIS Programming</category></item><item><title>Using C# 2.0 iterators to simplify writing asynchronous code (part 2)</title><link>http://blogs.msdn.com/michen/archive/2006/04/01/using-c-2-0-iterators-to-simplify-writing-asynchronous-code-part-2.aspx</link><pubDate>Sat, 01 Apr 2006 23:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566621</guid><dc:creator>michen</dc:creator><slash:comments>8</slash:comments><comments>http://blogs.msdn.com/michen/comments/566621.aspx</comments><wfw:commentRss>http://blogs.msdn.com/michen/commentrss.aspx?PostID=566621</wfw:commentRss><wfw:comment>http://blogs.msdn.com/michen/rsscomments.aspx?PostID=566621</wfw:comment><description>&lt;P&gt;&lt;A href="http://blogs.msdn.com/michen/archive/2006/03/30/564671.aspx" mce_href="http://blogs.msdn.com/michen/archive/2006/03/30/564671.aspx"&gt;Previous article&lt;/A&gt; describes the idea of using C# 2.0 iterators to write asynchronous code, now it's time to implement the utility class that "runs" the iterator. The utility turns out to be very light, I'm glad some readers reported they've already implemented their own version.&lt;/P&gt;
&lt;P&gt;Lets list requirements for the utility:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The user code should run sequentially. The code can "jump" between threads, but the main benefit of the approach is to let user write sequential code. &lt;/LI&gt;
&lt;LI&gt;The iterator should only resume when all the asynchronous operations finished, and previous &lt;FONT face="Courier New"&gt;moveNext()&lt;/FONT&gt; has yield the result. &lt;/LI&gt;
&lt;LI&gt;The utility should provide centralized exception handling. &lt;/LI&gt;
&lt;LI&gt;If the iterator function has &lt;FONT face="Courier New"&gt;finally&lt;/FONT&gt; clause, C# converts it to &lt;FONT face="Courier New"&gt;Dispose()&lt;/FONT&gt;&lt;FONT face=Courier&gt; &lt;/FONT&gt;method of &lt;FONT face="Courier New"&gt;IDisposable &lt;/FONT&gt;interface. We need to call it at the end of iteration.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;I've played with different ways to use the utility, and settled on the following pattern:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#800000&gt;AsyncEnumerator&lt;/FONT&gt; ae = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#800000&gt;AsyncEnumerator&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;();&lt;BR&gt;ae.Start(UserFunction(/*user params*/..., ae));&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;The &lt;FONT face="Courier New"&gt;&lt;FONT color=#800000&gt;AsyncEnumerator&lt;/FONT&gt; &lt;/FONT&gt;class exposes following public API:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR: blue; FONT-FAMILY: Courier New"&gt;public &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;sealed&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class &lt;/SPAN&gt;&lt;SPAN style="COLOR: maroon"&gt;AsyncEnumerator&lt;BR&gt;&lt;/SPAN&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; AsyncEnumerator() {}&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // The user-supplied exception handler, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if it returns true the enumeration stops.&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;Predicate&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: maroon"&gt;Exception&lt;/SPAN&gt;&amp;gt; Catch&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;&amp;nbsp;// User should never call this delegate directly, but&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // &lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;FONT color=#008000&gt;should pass this callback&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;FONT color=#008000&gt;to any async API it calls.&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public &lt;/SPAN&gt;&lt;SPAN style="COLOR: teal"&gt;AsyncCallback&lt;/SPAN&gt; Callback&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Start(&lt;SPAN style="COLOR: teal"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: teal"&gt;Int32&lt;/SPAN&gt;&amp;gt; enumerable) {}&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Now how do we run the user code sequentially, and only resume it after all async operations have finished? The first iteration is invoked directly (i.e. we simply call &lt;FONT face="Courier New"&gt;enumerator.MoveNext&lt;/FONT&gt;, but to make the Start return immediately, we do it on thread pool thread). To decide when to invoke following iterations, let's keep a counter of outstanding async operations (this value it returned by user's&amp;nbsp;&lt;FONT face="Courier New"&gt;yield return&lt;FONT face=Arial&gt;&amp;nbsp;statement and is accessible by&lt;/FONT&gt;&amp;nbsp;enumerator.Current&lt;/FONT&gt;). When&amp;nbsp;an async operation finishes and our callback is called, we decrement this counter by 1 (of course, we should use interlocked operations since callbacks may get called in parallel). Once the counter reaches 0, all the outstanding async operations have completed and we should resume the iterator (i.e. call &lt;FONT face="Courier New"&gt;enumerator.MoveNext&lt;/FONT&gt; again).&lt;/P&gt;
&lt;P&gt;The only tricky part is that callback(s) may get called before the iterator returns from &lt;FONT face="Courier New"&gt;MoveNext()&lt;/FONT&gt; call - this is why I've added the requirement that iterator can only be resumed after &lt;FONT face="Courier New"&gt;moveNext()&lt;/FONT&gt; has returned. The callback might even be called on the same thread that called the asynchronous method - if there is enough information to complete this call synchronously. The way we handle it is to&amp;nbsp;allow the number of outstanding async operations be negative - we start with 0; each async callback decrements the count by 1; and when &lt;FONT face="Courier New"&gt;iterator.MoveNext()&lt;/FONT&gt; returns we increment it by &lt;FONT face="Courier New"&gt;iterator.Current&lt;/FONT&gt;. If after any of these operations the number of outstanding async operations again becomes 0, we know that all async operations have finished and the iterator code has exited with 'yield return', so it is safe to advance the iterator again:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus there is a race between two threads&amp;nbsp;that may resume iterator: the original thread that called previous &lt;FONT face="Courier New"&gt;iterator.MoveNext()&lt;/FONT&gt;&amp;nbsp;and the thread that reported end of the last async operation. But it's OK, since our contract allows iterator code to migrate between threads, we only guarantee that a single thread runs it at a time.&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR: blue; FONT-FAMILY: Courier New"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt; &lt;SPAN style="COLOR: blue"&gt;sealed&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;AsyncEnumerator&lt;BR&gt;&lt;/SPAN&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;IEnumerator&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: teal"&gt;Int32&lt;/SPAN&gt;&amp;gt; m_enumerator;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;Int32&lt;/SPAN&gt; m_PendingAsyncOps = 0;&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Start(&lt;SPAN style="COLOR: teal"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: teal"&gt;Int32&lt;/SPAN&gt;&amp;gt; enumerable)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_enumerator = enumerable.GetEnumerator();&lt;BR&gt;&lt;BR&gt;&lt;EM&gt;&lt;SPAN style="COLOR: green"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // let's start the enumeration asynchronously too&lt;/SPAN&gt;&lt;BR&gt;&lt;/EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: maroon"&gt;ThreadPool&lt;/SPAN&gt;.QueueUserWorkItem(&lt;SPAN style="COLOR: blue"&gt;delegate&lt;/SPAN&gt; { Advance(); });&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;I&gt;&lt;FONT color=#008000&gt;// This is first implementation of Advance, to be revised below.&lt;BR&gt;&lt;/FONT&gt;&lt;/I&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Advance()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;while&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!m_enumerator.MoveNext())&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;EM&gt;&lt;SPAN style="COLOR: green"&gt;// The enumerator returns the number of async ops it initiated&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// Add this to m_PendingAsyncOps&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; pendingAsyncOps = &lt;SPAN style="COLOR: maroon"&gt;Interlocked&lt;/SPAN&gt;.Add(&lt;SPAN style="COLOR: blue"&gt;ref&lt;/SPAN&gt; m_PendingAsyncOps, m_enumerator.Current);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (pendingAsyncOps != 0) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;; &lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;//&amp;nbsp;quit this thread, let other thread resume iteration&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;/* No pending Async Ops, continue enumeration */&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;&lt;EM&gt;//&amp;nbsp;The delegate for&amp;nbsp;this function is passed to all async calls that&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // user makes and thus it is automatically called by async API&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // when an async operation finishes.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // It is never invoked by our code or directly by user.&lt;/EM&gt;&lt;EM&gt;&lt;BR&gt;&lt;/EM&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Advance(&lt;SPAN style="COLOR: teal"&gt;IAsyncResult&lt;/SPAN&gt; ar)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// An async op completed, subtract 1 from m_PendingAsyncOps&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: teal"&gt;Int32&lt;/SPAN&gt; pendingAsyncOps = &lt;SPAN style="COLOR: maroon"&gt;Interlocked&lt;/SPAN&gt;.Decrement(&lt;SPAN style="COLOR: blue"&gt;ref&lt;/SPAN&gt; m_PendingAsyncOps);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (m_PendingAsyncOps == 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// The last pending async op completed, continue enumeration&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Advance();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Finally, last requirements: exception hanlding and calling Dispose() method at the end of iteration. Both changes only affect &lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Advance() &lt;/SPAN&gt;function that works with iterator - we need to catch the exception and call the user-provider handler, and at the end of iteration call Dispose():&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Advance()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt; end = &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// Assume that we'll stop the iteration&lt;BR&gt;&lt;/EM&gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;while&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;try&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end = !m_enumerator.MoveNext();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;catch&lt;/SPAN&gt; (&lt;SPAN style="COLOR: maroon"&gt;Exception&lt;/SPAN&gt; e)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;EM&gt;&lt;SPAN style="COLOR: green"&gt;// An exception occurred, catch it and execute the callback.&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// The callback returns true to end the iteration or false to continue it&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end = (m_catch != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) ? m_catch(e) : &lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (end)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;;&lt;BR&gt;&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;EM&gt;&lt;SPAN style="COLOR: green"&gt;// The enumerator returns the number of async ops it initiated&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// Add this to m_PendingAsyncOps&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; pendingAsyncOps = &lt;SPAN style="COLOR: maroon"&gt;Interlocked&lt;/SPAN&gt;.Add(&lt;SPAN style="COLOR: blue"&gt;ref&lt;/SPAN&gt; m_PendingAsyncOps, m_enumerator.Current);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (pendingAsyncOps != 0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;break&lt;/SPAN&gt;; &lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;//&amp;nbsp;quit this thread, let other thread resume iteration&lt;/EM&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;/* No pending Async Ops, continue enumeration */&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (end)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// If nothing else to do, execute iterator's finally code&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: teal"&gt;IDisposable&lt;/SPAN&gt; d = m_enumerator &lt;SPAN style="COLOR: blue"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;IDisposable&lt;/SPAN&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (d != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) d.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Now the only missing piece is the public API. We have public Callback property that returns AsyncCallback delegate - we just return the second &lt;SPAN style="FONT-FAMILY: Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Advance(&lt;SPAN style="COLOR: teal"&gt;IAsyncResult&lt;/SPAN&gt; ar)&lt;/SPAN&gt; function, but to avoid creating new delegate for each async call, lets cache it. And exception handler is just a delegate that user can provide:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR: blue; FONT-FAMILY: Courier New"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: Courier New"&gt; &lt;SPAN style="COLOR: blue"&gt;sealed&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: maroon"&gt;AsyncEnumerator&lt;BR&gt;&lt;/SPAN&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;Predicate&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: maroon"&gt;Exception&lt;/SPAN&gt;&amp;gt; m_catch;&lt;BR&gt;&lt;BR&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;EM&gt;&lt;SPAN style="COLOR: green"&gt;// Delegate for passing to async methods&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/EM&gt;&lt;SPAN style="COLOR: green"&gt;&lt;EM&gt;// (to avoid creating new delegate for each call).&lt;BR&gt;&lt;/EM&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;AsyncCallback&lt;/SPAN&gt; callback;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; AsyncEnumerator() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.callback = Advance;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;EM&gt;The user-supplied exception handler, should return true to stop enumerator&lt;/EM&gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;Predicate&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: maroon"&gt;Exception&lt;/SPAN&gt;&amp;gt; Catch&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; m_catch; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt; { m_catch = &lt;SPAN style="COLOR: blue"&gt;value&lt;/SPAN&gt;; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;summary&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;EM&gt;The callback that user code should pass to&amp;nbsp;all async&amp;nbsp;APIs&lt;/EM&gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: gray"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/summary&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;AsyncCallback&lt;/SPAN&gt; Callback&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.callback; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;This is all for today. For next blog entry, I plan to do performance measurements, and compare code that uses this utility with code that uses threads and synchronous calls. But I've not done this yet, so you are welcome to post your results!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=566621" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Using C# 2.0 iterators to simplify writing asynchronous code</title><link>http://blogs.msdn.com/michen/archive/2006/03/30/using-c-2-0-iterators-to-simplify-writing-asynchronous-code.aspx</link><pubDate>Thu, 30 Mar 2006 11:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:564671</guid><dc:creator>michen</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/michen/comments/564671.aspx</comments><wfw:commentRss>http://blogs.msdn.com/michen/commentrss.aspx?PostID=564671</wfw:commentRss><wfw:comment>http://blogs.msdn.com/michen/rsscomments.aspx?PostID=564671</wfw:comment><description>&lt;P&gt;&lt;EM&gt;&lt;FONT face=Verdana&gt;A neat idea how C# 2.0 iterators can simplify the task of writing code that uses .NET async pattern.&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;I’ve recently attended &lt;A href="http://www.wintellect.com/TechnicalBioDetail.aspx?Tech=3" mce_href="http://www.wintellect.com/TechnicalBioDetail.aspx?Tech=3"&gt;Jeffrey Richter’s&lt;/A&gt;&amp;nbsp;class dedicated to effective threading techniques, and that made me think about using the .NET async pattern. The typical usage of this pattern is the following: you start one or more asynchronous operations using obj.BeginOperation method (e.g. stream.BeginRead) passing a callback delegate and immediately&amp;nbsp;return, releasing the thread that started processing.&amp;nbsp;Once the async&amp;nbsp;operation completes, your callback is called (usually on a thread-pool thread).&amp;nbsp;The callback code&amp;nbsp;checks what async operations have completed, and if you have enough data to proceed further&amp;nbsp;it executes next block of code. This is very important for multithreaded servers, since very little resources are being used while the program waits for operation to finish (which can take a while).&lt;/P&gt;
&lt;P&gt;But it is very hard to program with this pattern - instead of writing simple sequential code, the code is split into disconnected callbacks that may get executed in parallel. In some cases anonymous delegates make it somewhat better. But if more than one async operation is to be performed in sequence you usually need to maintain some state machine, evaluate where you are and what operation is to be performed next, etc - complex and buggy. Jeffry Richter showed some sample code doing this, but it is obvious it is very hard to write and maintain&amp;nbsp;such code.&lt;/P&gt;
&lt;P&gt;I thought it would be nice if there was a way to write sequential code that is put asleep (but does not waste thread), and then restarted when the async operation completes - so called coroutines (see &lt;A href="http://en.wikipedia.org/wiki/Coroutines" mce_href="http://en.wikipedia.org/wiki/Coroutines"&gt;http://en.wikipedia.org/wiki/Coroutines&lt;/A&gt;). Then I realized we already have these in C# 2.0 - in form of C# iterators! The idea is to organize the application code as an "iterator" that will "yield" some value after each async operation. Then we need a library utility class that restarts the iterator after the next async operation finishes.&lt;/P&gt;
&lt;P&gt;The user code will looks like this:&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008080 size=2&gt;IEnumerable&lt;/FONT&gt;&amp;lt;&lt;FONT color=#008080&gt;int&lt;/FONT&gt;&amp;gt; Foo(&lt;FONT color=#008000&gt;/* user params */&lt;/FONT&gt;..., &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000 size=2&gt;/* utility class */ &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;AsyncEnumerator&lt;/FONT&gt; ae)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;// start async operation&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IAsyncResult asyncResult = stream.BeginRead(..., &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;/* callback provided by utility*/&lt;/FONT&gt; ae.Advance, &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;null&lt;/FONT&gt;&lt;FONT size=2&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#008000&gt;//&amp;nbsp;we could do some more stuff here before yielding&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;yield&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; 1;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // the code below is called after the code above&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;FONT color=#008000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;and&amp;nbsp;after the asynchronous operation ends&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; dataRead = stream.EndRead(asyncResult);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;and is started by following code:&lt;/P&gt;&lt;FONT size=2&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#008080 size=2&gt;AsyncEnumerator&lt;/FONT&gt;&lt;FONT size=2&gt; ae = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;AsyncEnumerator&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;ae.Start(Foo(&lt;FONT color=#008000&gt;/* user params */ ...&lt;/FONT&gt;, ae));&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;The &lt;FONT face="Courier New"&gt;&lt;FONT color=#008080&gt;AsyncEnumerator&lt;/FONT&gt; &lt;/FONT&gt;utility guarantees the code is executed sequentially, although it may jump between threads :). My iterator code originally yield the IAsyncResult returned by async operation&amp;nbsp;it just started, but it was not really used by utility class. I then showed it to Jeffrey Richter who suggested an ingenious idea - the iterator should produce the number of outstanding async operations started by the iterator code - and the utility class resumes the iterator when all of&amp;nbsp;these operations have completed, this idea fits very well with the synchronization code.&lt;/P&gt;
&lt;P&gt;Another issue solved by the utility is exception handling: C# iterators can't have catch block around yield statement, so centralized error handling is hard. The utility catches exceptions raised by iterator, and calls a user-provided delegate that performs exception handling and returns boolean value indicating whether the iteration should stop (another neat improvement by Jeffrey Richter).&lt;/P&gt;
&lt;P&gt;If this made you interested, come back to this blog - I'll post details on the inner working of the utility &lt;A class="" href="http://blogs.msdn.com/michen/archive/2006/04/01/using-c-2-0-iterators-to-simplify-writing-asynchronous-code-part-2.aspx" mce_href="http://blogs.msdn.com/michen/archive/2006/04/01/using-c-2-0-iterators-to-simplify-writing-asynchronous-code-part-2.aspx"&gt;in the next entry&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;References:&lt;BR&gt;C# 2.0 iterators &lt;A href="http://msdn.microsoft.com/msdnmag/issues/04/05/C20/" mce_href="http://msdn.microsoft.com/msdnmag/issues/04/05/C20/"&gt;http://msdn.microsoft.com/msdnmag/issues/04/05/C20/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Async method pattern in .NET &lt;BR&gt;&lt;A href="http://msdn.microsoft.com/msdnmag/issues/01/08/Async/" mce_href="http://msdn.microsoft.com/msdnmag/issues/01/08/Async/"&gt;http://msdn.microsoft.com/msdnmag/issues/01/08/Async/&lt;/A&gt;&lt;BR&gt;&lt;A href="http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/default.aspx" mce_href="http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/default.aspx"&gt;http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/default.aspx&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=564671" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/michen/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>