<?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>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><description>Previous article 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</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: 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#566784</link><pubDate>Sun, 02 Apr 2006 11:56:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566784</guid><dc:creator>Oleg Mihailik</dc:creator><description>Michael, I think your implementation of this idea is a little over-complex.&lt;br&gt;&lt;br&gt;By the way, with Google and &amp;quot;Asynchronous iterators&amp;quot; query you can find:&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://www.google.com/search?q=asynchronous+iterators"&gt;http://www.google.com/search?q=asynchronous+iterators&lt;/a&gt;&lt;br&gt;&lt;br&gt;1. My article on this idea: msmvps.com/blogs/79813.aspx&lt;br&gt;&lt;br&gt;2. Your first article: blogs.msdn.com/michen/archive/2006/03/30/564671.aspx&lt;br&gt;&lt;br&gt;3. Miguel de Icaza post about asynchronuos execution and iterators in Mono ASP.NET implementation.&lt;br&gt;&lt;br&gt;And you can find, Miguel's post was first. And the Mono project is first place where this cool idea was used in C#. Strangely, but nobody detected that cool idea in Miguel's blog.&lt;br&gt;&lt;br&gt;In my post (at Christmas) I had no feedback too.&lt;br&gt;&lt;br&gt;I think the idea is very competitive, but the implementation and framework around it should be a lot stronger and simpler to be widely used. May be such asynchronous &amp;quot;chained&amp;quot; operation is good place for improvement at language level in future C# versions.</description></item><item><title>re: 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#566790</link><pubDate>Sun, 02 Apr 2006 12:42:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566790</guid><dc:creator>michen</dc:creator><description>Very interesting, I should have googled before posting this :)&lt;br&gt;&lt;br&gt;Indeed, yours and Miguel de Icaza blogs describe almost the same usage as I described.&lt;br&gt;&lt;br&gt;I believe the synchronization logic is necessary though - C#-generated iterator is not thread-safe, and both implementations (at least as described in blogs) suffer from the problem solved in my article: they do not handle correctly the case when async operation finishes before or at the same time when the thread that started it produces the value with 'yield return'. And this is not just theoretical problem - I've found this could happen with HttpWebRequest class.&lt;br&gt;&lt;br&gt;The complexity of implementation is arguable, but with some added complexity in the library, it lets one write simpler code: by centralizing exception handling, ensuring finally block in the iterator is called, and avoiding unnecessary anonymous delegates in user code – the user writes simple sequential code (this is also the case for Miguel's version).&lt;br&gt;&lt;br&gt;But anyway it is the same idea, thanks for letting me know – it was very educating to look at other implementations! I'm adding your and Miguels blogs to my RSS agregator :)</description></item><item><title>re: 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#566803</link><pubDate>Sun, 02 Apr 2006 13:36:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566803</guid><dc:creator>Oleg Mihailik</dc:creator><description>Michael, I understan you very well :-)&lt;br&gt;&lt;br&gt;Actually I found previous Miguel's implementation AFTER my posting too. It was so sad at first time!&lt;br&gt;&lt;br&gt;---&lt;br&gt;&lt;br&gt;About threading issues.&lt;br&gt;Actually, my implementation is not fragile by race conditions. Let me show why.&lt;br&gt;&lt;br&gt;Look here, it is asynchronous algorithm of Socket.BeginAccept/EndAccept:&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=a9557a71-8e52-48ef-b867-d0f78e636e77"&gt;http://blogs.gotdotnet.ru/personal/mihailik/PermaLink.aspx?guid=a9557a71-8e52-48ef-b867-d0f78e636e77&lt;/a&gt;&lt;br&gt;&lt;br&gt;The core is iterator:&lt;br&gt;&lt;br&gt;private IEnumerator&amp;lt;Invoke&amp;gt; AsyncListen(Action&amp;lt;Socket&amp;gt; yieldSocket, Invoke next)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;while( true )&lt;br&gt; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IAsyncResult acceptCompleted = null;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return delegate&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;acceptCompleted = tcpListener.BeginAcceptSocket(&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;delegate { next(); },&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;null);&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;};&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Socket acceptedSocket = tcpListener.EndAcceptSocket(acceptCompleted);&lt;br&gt;&lt;br&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yieldSocket(acceptedSocket);&lt;br&gt; &amp;nbsp; &amp;nbsp;}&lt;br&gt;}&lt;br&gt;&lt;br&gt;Here you can see that it returns not IEnumerable &amp;quot;of int&amp;quot;, but &amp;quot;of Invoke&amp;quot;, and Invoke is actually delegate.&lt;br&gt;&lt;br&gt;So, the things going more complex under the cover, but synctactic sugar hides this. Here is different parts of algorithm:&lt;br&gt;&lt;br&gt;1. Preparing to Socket.BeginAccept&lt;br&gt;2. Calling Socket.BeginAccept&lt;br&gt;3. Stepping by IEnumerator.MoveNext&lt;br&gt;4. Calling Socket.EndAccept&lt;br&gt;&lt;br&gt;Actually iterator code contains only (1) and (4).&lt;br&gt;&lt;br&gt;BeginAccept (3) is not called from iterator code, but it is called from surround &amp;quot;framework&amp;quot; code. As you can see, iterator yields asynchronous delegate with BeginAccept call inside it. Iterator does not call BeginAccept, it is just delegate this call to surround &amp;quot;framework&amp;quot;.&lt;br&gt;&lt;br&gt;So, when iterator's code is stopped by yield, BeginInvoke is yet not called. It means iterator NEVER can execute next step BEFORE finishing priveous.&lt;br&gt;&lt;br&gt;* * *&lt;br&gt;&lt;br&gt;Also I have some ideas for C# language features for this problem. It may be automatic BeginOperation/EndOperation implementation, based on Operation() method.&lt;br&gt;&lt;br&gt;Compiler may detect the places for &amp;quot;yield-step-points&amp;quot; inside hand-written Operation() method. For example:&lt;br&gt;&lt;br&gt;ParsedRequest GetRequest(Socket socket)&lt;br&gt;{&lt;br&gt; &amp;nbsp; &amp;nbsp;...&lt;br&gt; &amp;nbsp; &amp;nbsp;Socket.Read(...);&lt;br&gt; &amp;nbsp; &amp;nbsp;...&lt;br&gt; &amp;nbsp; &amp;nbsp;return ...&lt;br&gt;}&lt;br&gt;&lt;br&gt;The compiler may found calls to Socket.Read, and convert it to Socket.BeginRead/EndRead, splitted by yield-like-continuation.&lt;br&gt;&lt;br&gt;Syntactically it may be something like:&lt;br&gt;&lt;br&gt;async ParsedRequest GetRequest(Socket socket)&lt;br&gt;&lt;br&gt;and it will be compiled into three methods: actual GetRequest and pair of BeginGetRequest/EndGetRequest generated by compiler.&lt;br&gt;</description></item><item><title>re: 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#566816</link><pubDate>Sun, 02 Apr 2006 14:03:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566816</guid><dc:creator>michen</dc:creator><description>Thanks, I now understand how you code works - yes, it looks thread-safe.&lt;br&gt;&lt;br&gt;***&lt;br&gt;&lt;br&gt;About C# compiler doing the code transformation - this was exactly the same wish I've got too :)&lt;br&gt;&lt;br&gt;The second thought was that LISP with its (arguably evil, but powerful) macro facility would allow implementing this in a library, rather than modifying core language. Unfortunately, I don't see a way to have LISP-like macro for a language with complex syntax like C#.</description></item><item><title>Interesting Finds</title><link>http://blogs.msdn.com/michen/archive/2006/04/01/using-c-2-0-iterators-to-simplify-writing-asynchronous-code-part-2.aspx#566820</link><pubDate>Sun, 02 Apr 2006 14:23:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:566820</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>Interesting Finds</title><link>http://blogs.msdn.com/michen/archive/2006/04/01/using-c-2-0-iterators-to-simplify-writing-asynchronous-code-part-2.aspx#567145</link><pubDate>Mon, 03 Apr 2006 12:28:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:567145</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>re: 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#2031660</link><pubDate>Thu, 05 Apr 2007 12:52:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2031660</guid><dc:creator>jasons</dc:creator><description>&lt;p&gt;you both rock, my hat is off to both of you sirs!&lt;/p&gt;
&lt;p&gt;thank you for your conversation, i find it very interesting and compelling!&lt;/p&gt;
</description></item><item><title>re: 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#2402569</link><pubDate>Fri, 04 May 2007 03:45:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2402569</guid><dc:creator>John</dc:creator><description>&lt;p&gt;Thanks for a great article. &amp;nbsp;Most of the examples I've seen for using sockets don't deal with the real life issues such as the one you mention with HttpWebRequest. &amp;nbsp;M(ost examples don't deal with packet fragmentation either, simply assuming that any aribitrary number of bytes they read is the entire message and can be converted to a string with a single call to an encoding function.) &amp;nbsp;I appreciate the complication that you added to your code to make it more realistic.&lt;/p&gt;</description></item></channel></rss>