<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.microsoft.co.il/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>Bnaya Eshet : Task</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx</link><description>Tags: Task</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Async / Await for .NET 4, Silverlight and Windows Phone</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/05/07/async-await-for-net-4-silverlight-and-windows-phone.aspx</link><pubDate>Tue, 07 May 2013 09:25:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:2004418</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Async / Await for .NET 4, Silverlight and Windows Phone&lt;/h2&gt;  &lt;p&gt;if you have to target .NET 4, Silverlight and Windows Phone and    &lt;br /&gt;still want to use the &lt;strong&gt;async / await&lt;/strong&gt; pattern. &lt;/p&gt;  &lt;p&gt;the BCL team provide you with a new NuGet package named &lt;strong&gt;Microsoft.Bcl.Async&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0368_37D8F7B8.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Parallel.Async, await, Task, TPL, BCL" border="0" alt="Parallel.Async, await, Task, TPL, BCL" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0368_thumb_3995D55A.jpg" width="463" height="336" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;this package was announced as &lt;strong&gt;stable&lt;/strong&gt; a few week ago.     &lt;br /&gt;so you can check it out if you&amp;#39;re having VS 2012 but should target one of the above platforms.&lt;/p&gt;  &lt;p&gt;be aware that this package won&amp;#39;t work with 2010.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=2004418" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/BCL/default.aspx">BCL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel.Async/default.aspx">Parallel.Async</category></item><item><title>EF 6: Async</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/03/19/ef-6-async.aspx</link><pubDate>Tue, 19 Mar 2013 15:02:51 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1811777</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;EF 6: Async&lt;/h2&gt;  &lt;p&gt;this post is the first in a series about what&amp;#39;s new in &lt;strong&gt;EF 6&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;great improvements are about to come with &lt;strong&gt;Entity Framework 6&lt;/strong&gt;.     &lt;br /&gt;it is a major release and the first one since EF become an open source.     &lt;br /&gt;each post in the series will be dedicate to a single feature.&lt;/p&gt;  &lt;p&gt;this post will focus on a new&lt;strong&gt; EF a-sync&lt;/strong&gt; features.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_1416_38605C8C.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="EF, Parallel, async, await, task, api, entity" border="0" alt="EF, Parallel, async, await, task, api, entity" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_1416_thumb_7DE49B10.jpg" width="508" height="351" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the first question that should be asked is, why do we need &lt;strong&gt;parallel data access&lt;/strong&gt;?     &lt;br /&gt;moreover why do we need a &lt;strong&gt;dedicate parallel data access API&lt;/strong&gt;, rather then using the TPL Task.Run(…) in order to introduce parallelism?&lt;/p&gt;  &lt;p&gt;the answer is related both to &lt;strong&gt;thread safety &lt;/strong&gt;and &lt;strong&gt;ThreadPool&lt;/strong&gt; &lt;strong&gt;optimization&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;a &lt;strong&gt;dedicate data access API&lt;/strong&gt; for &lt;strong&gt;parallel&lt;/strong&gt; programming, ensured a &lt;strong&gt;thread safe access&lt;/strong&gt;, which may not be a trivial task.&lt;/p&gt;  &lt;p&gt;furthermore, when the database is executing a query the .NET thread is idle (waiting for the execution result).    &lt;br /&gt;if this idle thread has taken from &lt;strong&gt;ThreadPool&lt;/strong&gt; (either directly or through high level API like TPL Task.Run(…)) it could lead to a ThreadPool &lt;strong&gt;starvation&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;assuming that you have a 2 second query, Task.Run(…) will hold the ThreadPool&amp;#39;s thread for 2 second even though it idle while the database is executing the query.&lt;/p&gt;  &lt;p&gt;what you really want is an &lt;strong&gt;API that operate over IOCP&lt;/strong&gt; (IO completion port). &lt;strong&gt;IOCP&lt;/strong&gt; &lt;strong&gt;API&lt;/strong&gt; will release the ThreadPool&amp;#39;s thread back to the pool and &lt;strong&gt;re-claim&lt;/strong&gt; &lt;strong&gt;a thread&lt;/strong&gt; at the completion time.&lt;/p&gt;  &lt;p&gt;at the API level you can find &lt;strong&gt;a-sync operations everywhere&lt;/strong&gt;.     &lt;br /&gt;I will present some of the main API.&lt;/p&gt;  &lt;p&gt;the following snippets, will use the following &lt;strong&gt;entities&lt;/strong&gt; (code first):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fbe34654-313a-4d52-8dce-d057a79ee95b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Author&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Author()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;Blogs = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;List&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Blog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Id { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Name { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;IList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Blog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; Blogs { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Blog&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Id { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Title { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Content { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; AuthorId { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;and the following &lt;strong&gt;context&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:28fbb720-798b-4adc-b525-a499891febb1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Context&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; : &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DbContext&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DbSet&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Author&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; Authors { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;DbSet&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Blog&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; Blogs { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;get&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;set&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;; }&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;a-sync save&lt;/h5&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:83e348f2-9cd6-4ca0-9de9-860ef135b5f8" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; context = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Context&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;())&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;context.Authors.Add(author);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; affected = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; context.SaveChangesAsync();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;notice the &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/29/the-concept-of-async-await.aspx"&gt;await&lt;/a&gt; at line 4.&lt;/p&gt;  &lt;h5&gt;a-sync read&lt;/h5&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1f2f32d3-f7d9-42c6-99d2-f65460397626" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; context = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Context&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;())&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Author&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] authors = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; context.Authors&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;.Include(a =&amp;gt; a.Blogs)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;.ToArrayAsync();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; authors;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 5&lt;/strong&gt;, is where the magic happens.&lt;/p&gt;  &lt;h5&gt;execute command&lt;/h5&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:115f7c17-c8a6-4787-9ef2-d31583d09f3f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;using&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; context = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Context&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;())&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; affected = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;context.Database.ExecuteSqlCommandAsync(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;DELETE FROM BLOGS&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;EF 6&lt;/strong&gt; is having a great parallel API, which respect &lt;strong&gt;IOCP&lt;/strong&gt; and compatible with the new &lt;strong&gt;async / await&lt;/strong&gt; syntax. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.seladeveloperpractice.com/"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="EF, Parallel, async, await, task, api, entity, sdp" border="0" alt="EF, Parallel, async, await, task, api, entity,sdp" src="http://blogs.microsoft.co.il/blogs/bnaya/sdp-2013_7B87C6BA.png" width="316" height="125" /&gt;&lt;/a&gt;     &lt;br /&gt;&lt;/p&gt; &lt;a href="http://dotnetshoutout.com/EF-6-Async-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F03%2F19%2Fef-6-async.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1811777" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/EF/default.aspx">EF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/api/default.aspx">api</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/entity/default.aspx">entity</category></item><item><title>Parallel and The C# Memory Model</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/03/06/parallel-and-the-c-memory-model.aspx</link><pubDate>Wed, 06 Mar 2013 06:46:13 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1781981</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Parallel and The C# Memory Model&lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Parallel&lt;/strong&gt; programming &lt;strong&gt;can be tricky&lt;/strong&gt;, both compiler and CPU&amp;#39;s optimization can lead into a twilight zone&amp;#39;s debugging.&lt;/p&gt;  &lt;p&gt;lets take the following code snippet snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ad6499c3-499f-4e52-9308-8dc1c0e3c6d4" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Program&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Main(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;string&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[] args)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Start&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; u = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Util&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;u.Exec();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.ReadKey();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Util&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; _stop = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; Exec()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Run(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;bool&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; b = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;true&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;while&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (_stop)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;b = !b;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;Complete {0}&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, b);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Thread&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Sleep(30);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;_stop = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;false&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;can you predict the outcome?    &lt;br /&gt;will it ever reach the completion at line 24?&lt;/p&gt;  &lt;p&gt;you can download the snippet from &lt;a href="http://sdrv.ms/Y9Tu5t"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;now &lt;a href="http://sdrv.ms/Y9Tu5t"&gt;download&lt;/a&gt; the snippet and try to execute it in the following order:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Compiled in &lt;strong&gt;Debug&lt;/strong&gt; mode and &lt;strong&gt;double click&lt;/strong&gt; on the &lt;strong&gt;exe&lt;/strong&gt; file (without an attached debugger). &lt;/li&gt;    &lt;li&gt;Compiled in &lt;strong&gt;Release &lt;/strong&gt;mode and &lt;strong&gt;double click&lt;/strong&gt; on the &lt;strong&gt;exe&lt;/strong&gt; file (without an attached debugger). &lt;/li&gt;    &lt;li&gt;Compiled in &lt;strong&gt;Release &lt;/strong&gt;mode and run it &lt;strong&gt;F5&lt;/strong&gt; (with an attached debugger). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0713_62B4BA7F.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Parallel, Task, Memory model, MemoryBarrier, rase, optimization, reordering" border="0" alt="Parallel, Task, Memory model, MemoryBarrier, rase, optimization, reordering" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0713_thumb_4D0A5C15.jpg" width="500" height="411" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;so why does is execute &lt;strong&gt;predictably&lt;/strong&gt; in &lt;strong&gt;Debug mode&lt;/strong&gt;, while behaving quit &lt;strong&gt;strange&lt;/strong&gt; in &lt;strong&gt;Release mode&lt;/strong&gt;?     &lt;br /&gt;even stranger why does it execute &lt;strong&gt;predictably&lt;/strong&gt; in &lt;strong&gt;Release mode&lt;/strong&gt; when a &lt;strong&gt;debugger attached&lt;/strong&gt;?&lt;/p&gt;  &lt;p&gt;what happening is a matter of a &lt;strong&gt;single thread optimization&lt;/strong&gt; that can occurs at the &lt;strong&gt;compiler (JIT)&lt;/strong&gt; or &lt;strong&gt;CPU&lt;/strong&gt; level which is taking &lt;strong&gt;assumption which is not acceptable for parallel execution&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;it can be &lt;strong&gt;fixed&lt;/strong&gt; up using different API like &lt;strong&gt;Thread.MemoryBarrier&lt;/strong&gt; and other.&lt;/p&gt;  &lt;p&gt;instead of explaining this out, I will offer you a reading of &lt;a href="http://msdn.microsoft.com/en-us/magazine/jj863136.aspx"&gt;part 1&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/magazine/jj883956.aspx"&gt;part 2&lt;/a&gt; of a&lt;strong&gt; great article&lt;/strong&gt; on those matters. &lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;the &lt;strong&gt;optimization&lt;/strong&gt; world is still &lt;strong&gt;rely on a single thread assumption&lt;/strong&gt; (with some special instruction for parallel execution).     &lt;br /&gt;while the computing world becoming more parallel each day, this single thread assumption priority may have to be change in the future.&lt;/p&gt;  &lt;p&gt;in order to avoid pitfalls, you should be aware o it and use a &lt;strong&gt;parallel dedicated APIs&lt;/strong&gt;.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Parallel-and-The-C-Memory-Model-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F03%2F06%2Fparallel-and-the-c-memory-model.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1781981" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/continuation/default.aspx">continuation</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Concurrency/default.aspx">Concurrency</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/rase/default.aspx">rase</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/optimization/default.aspx">optimization</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/MemoryBarrier/default.aspx">MemoryBarrier</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/reordering/default.aspx">reordering</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Memory+model/default.aspx">Memory model</category></item><item><title>Immutable Collections</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/02/17/immutable-collections.aspx</link><pubDate>Sun, 17 Feb 2013 19:45:12 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1751972</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Immutable Collections &lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a pattern which is suit well &lt;strong&gt;parallel programming&lt;/strong&gt;,     &lt;br /&gt;but you have to be aware of a potential &lt;strong&gt;memory pressure risk&lt;/strong&gt; when it&amp;#39;s not implemented right or used wisely.&lt;/p&gt;  &lt;p&gt;this post will cover a new &lt;strong&gt;BCL library&lt;/strong&gt; (still in its preview stage) which is targeting&lt;strong&gt; immutable collections&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_692BD55A.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0382_thumb_6CE97A2A.jpg" width="423" height="308" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;.NET&lt;/strong&gt; is already having &lt;strong&gt;Concurrent implementation&lt;/strong&gt; for Queue, Stack, Bug and Dictionary, which is &lt;strong&gt;thread-safe&lt;/strong&gt;, but other type of collection like List is missing.     &lt;br /&gt;another type of collection are &lt;strong&gt;read only collection&lt;/strong&gt;, but those type of collection are &lt;strong&gt;not truly thread-safe&lt;/strong&gt; because it is just a wrapper upon other collection which can be mutate by it&amp;#39;s owner.     &lt;br /&gt;so &lt;strong&gt;iterating on a read only collection doesn&amp;#39;t guarantee thread-safety&lt;/strong&gt; because the collection can be mutate during the iteration by it&amp;#39;s original owner.     &lt;br /&gt;currently we do have one option to create an &lt;strong&gt;immutable collection&lt;/strong&gt; by using the &lt;strong&gt;ToArray()&lt;/strong&gt; extension method, but as I mentioned before it &lt;strong&gt;can lead to a memory pressure&lt;/strong&gt; while using a large collection.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;BCL team&lt;/strong&gt; was targeting this issue and are working on a new kind of collections which are immutable.     &lt;br /&gt;those collection is having a cool concept which guarantee that an immutable sequence will not changed, but it can be part of another immutable sequence. this mean that you can take an immutable collection and add an item. this will keep the immutable part as is and you will get a new collection that include your addition and the original collection, but if someone is using the original collection he won&amp;#39;t be affected by your mutation.     &lt;br /&gt;as you can see the &lt;strong&gt;BCL immutable collection&lt;/strong&gt; &lt;strong&gt;can be&lt;/strong&gt; &lt;strong&gt;mutate&lt;/strong&gt; but the mutation will return a new collection and leave the original collection untouched.&lt;/p&gt;  &lt;h5&gt;Start using the immutable collection&lt;/h5&gt;  &lt;p&gt;you can start using it though &lt;a href="https://nuget.org/packages/Microsoft.Bcl.Immutable"&gt;NuGet&lt;/a&gt;.     &lt;br /&gt;on the current preview version it includes:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ImmutableStack&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableQueue&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableList&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableHashSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedSet&amp;lt;T&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt;    &lt;li&gt;ImmutableSortedDictionary&amp;lt;K, V&amp;gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Fluent API&lt;/h5&gt;  &lt;p&gt;I will speak about 3 APIs:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add &lt;/li&gt;    &lt;li&gt;AddRange &lt;/li&gt;    &lt;li&gt;Builder &lt;/li&gt; &lt;/ul&gt;  &lt;h6&gt;Add&lt;/h6&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6e44ba97-9c59-49e9-8793-4d1ed33706a6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddItem_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.Add(1);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col2 = col1.Add(2);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(2, col2.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;whenever you want an &lt;strong&gt;empty instance&lt;/strong&gt; of immutable collection you should use the &lt;strong&gt;static Empty property&lt;/strong&gt; (there is no public constructor available).&lt;/p&gt;  &lt;p&gt;you should always remember that Adding an item will &lt;strong&gt;not affect the original collection&lt;/strong&gt; therefore you must &lt;strong&gt;handle the return value&lt;/strong&gt; (is is the same concept as string.Replace).&lt;/p&gt;  &lt;p&gt;finally you can notice at lines 8-10 that the above code was actually construct 3 different immutable collections.&lt;/p&gt;  &lt;h6&gt;AddRange&lt;/h6&gt;  &lt;p&gt;both from &lt;strong&gt;performance&lt;/strong&gt; and &lt;strong&gt;usability&lt;/strong&gt; it is better using the &lt;strong&gt;AddRange&lt;/strong&gt; API when you intend to add multiple items.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4a59b070-234d-40da-af88-f5149f6ae735" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_AddRange_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ImmutableList&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;.Empty;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = col.AddRange(&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000));&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(0, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h6&gt;Builder&lt;/h6&gt;  &lt;p&gt;if you want to &lt;strong&gt;efficiently&lt;/strong&gt; &lt;strong&gt;add multiple items&lt;/strong&gt; and AddRange doesn&amp;#39;t do the job for you, you can use a &lt;strong&gt;Builder&lt;/strong&gt; (the concept is quit similar to StringBuilder).     &lt;br /&gt;you should remember that &lt;strong&gt;unlike the immutable collection a builder is not a thread-safe&lt;/strong&gt; structure, therefore you shouldn&amp;#39;t share its state between threads.     &lt;br /&gt;when you complete the mutation, you can &lt;strong&gt;extract an immutable collection from the builder&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:74e71650-2eaf-46b2-b766-eed18f1569aa" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;[&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Test&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;]&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;void&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; List_Builder_Test()&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Enumerable&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Range(0, 1000).ToImmutableList();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; builder = col.ToBuilder();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;for&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; i = 0; i &amp;lt; 100; i++)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;builder.Remove(i * 10);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; col1 = builder.ToImmutable();&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(1000, col.Count);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Assert&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AreEqual(900, col1.Count);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;line 5&lt;/strong&gt;: getting a builder from immutable collection.     &lt;br /&gt;&lt;strong&gt;line 10&lt;/strong&gt;: getting an immutable back from the builder (after the mutation).&lt;/p&gt;  &lt;h5&gt;Migration&lt;/h5&gt;  &lt;p&gt;if you are using a read only collection though the IReadOnlyCollection, IReadOnlyList or IReadOnlyDictionary, migration will be easy because the immutable collections are implementing those interfaces.&lt;/p&gt;  &lt;h5&gt;Optimization&lt;/h5&gt;  &lt;p&gt;we already spoke about the memory pressure optimization, but what is the characterize of those collection in compare with the well known BCL mutable collection?    &lt;br /&gt;as the BCL team argue that &lt;strong&gt;immutable collections have been heavily tuned for maximum performance and minimum GC pressure&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the following table was published by the &lt;strong&gt;BCL team&lt;/strong&gt;.     &lt;br /&gt;it &lt;strong&gt;compare the performance behavior of the BCL immutable and immutable&lt;/strong&gt; &lt;strong&gt;collection&lt;/strong&gt; (and remember that this is only a preview version which mean that further optimization can be applied).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_3C72F5B4.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Immutable, Parallel, Task, BCL, collection, Thread-safe" border="0" alt="Immutable, Parallel, Task, BCL, collection, Thread-safe" src="http://blogs.microsoft.co.il/blogs/bnaya/immutable-table_thumb_33A2ED68.png" width="492" height="179" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;you can read more on this at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Immutability&lt;/strong&gt; is a great pattern for &lt;strong&gt;parallelism&lt;/strong&gt;.     &lt;br /&gt;the BCL team are giving us a robust and &lt;strong&gt;well design immutable collection&lt;/strong&gt;.     &lt;br /&gt;the potential memory pressure has reduced by the internal data-structure design.     &lt;br /&gt;so it is definitely something to wait for its release.&lt;/p&gt;  &lt;p&gt;you can read more about it at &lt;a href="http://blogs.msdn.com/b/bclteam/archive/2012/12/18/preview-of-immutable-collections-released-on-nuget.aspx"&gt;here&lt;/a&gt;.     &lt;br /&gt;and if you want more background and comparison to the read only collection, you can check out &lt;a href="http://blogs.msdn.com/b/andrewarnottms/archive/2011/08/22/read-only-frozen-and-immutable-types-and-collections.aspx"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Immutable-Collections-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F02%2F17%2Fimmutable-collections.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1751972" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Extensibility/default.aspx">Extensibility</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/extension/default.aspx">extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Composition/default.aspx">Composition</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ImportMany/default.aspx">ImportMany</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Export/default.aspx">Export</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Import/default.aspx">Import</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/CompositionContainer/default.aspx">CompositionContainer</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/collection/default.aspx">collection</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/BCL/default.aspx">BCL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Immutable/default.aspx">Immutable</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread-safe/default.aspx">Thread-safe</category></item><item><title>Open House at Microsoft</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/14/open-house-at-microsoft.aspx</link><pubDate>Mon, 14 Jan 2013 07:48:22 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1660692</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Open House at Microsoft&lt;/h2&gt;  &lt;p&gt;yesterday I was lecturing at Microsoft about VS 2012, .NET 4.5, async/await, Rx and TPL Dataflow.&lt;/p&gt;  &lt;p&gt;there was 90 people attended and I hope that everybody has learn something new.&lt;/p&gt;  &lt;p&gt;the code sample for this lecture available &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%216027" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Ashdot-field-5_584BDCDB.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Parallel,tdf, dataflow, rx, reactive, task, .net 4., vs 2012" border="0" alt="Parallel,tdf, dataflow, rx, reactive, task, .net 4., vs 2012" src="http://blogs.microsoft.co.il/blogs/bnaya/Ashdot-field-5_thumb_21EA8EFF.jpg" width="468" height="364" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1660692" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Rx/default.aspx">Rx</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Dataflow/default.aspx">Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx">TDF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Reactive/default.aspx">Reactive</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Reactive+Extension/default.aspx">Reactive Extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/vs+2012/default.aspx">vs 2012</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net+4_2E00_/default.aspx">.net 4.</category></item><item><title>Async and AggregateException</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2013/01/14/async-and-aggregateexception.aspx</link><pubDate>Mon, 14 Jan 2013 06:19:42 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1660489</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Async and AggregateException&lt;/h2&gt;  &lt;p&gt;this post is a complementary to &lt;a href="http://blogs.microsoft.co.il/blogs/stiller/archive/2012/12/31/accessing-the-aggregateexception-with-await.aspx" target="_blank"&gt;Eran Stiller&amp;#39;s post&lt;/a&gt;.     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;I was reading &lt;strong&gt;Eran Stiller&amp;#39;s &lt;/strong&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/stiller/archive/2012/12/31/accessing-the-aggregateexception-with-await.aspx" target="_blank"&gt;post&lt;/a&gt; about &lt;strong&gt;exception handling&lt;/strong&gt; using &lt;strong&gt;async&lt;/strong&gt; methods and I want to add a few side notes.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0115_62725ED9.jpg"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="async, await, parallel, task, tpl, tap,.net4.5,c#5, exception" border="0" alt="async, await, parallel, task, tpl, tap,.net4.5,c#5, exception" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_0115_thumb_49D29BBC.jpg" width="497" height="381" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;1) the exception handling behavior decisions is well documented in &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;this&lt;/a&gt; post (by the TPL team), it decided after they had consider different options.&lt;/p&gt;  &lt;p&gt;2) I was suggesting that compiler will check whether the a catch of AggregateException is implemented and if so to avoid the unwrapping behavior.    &lt;br /&gt;in this case the it can be assumed that programmer do expect multiple exceptions.     &lt;br /&gt;unfortunately, for the current version, I was&amp;#160; speaking with them after the release of .NET 4.5.     &lt;br /&gt;I hope that we will see this mitigation in future release.&lt;/p&gt;  &lt;p&gt;3) for the current version you can use the following &lt;strong&gt;work around&lt;/strong&gt; if you do want to &lt;strong&gt;catch aggregate exception&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:083b3af6-dfc0-4562-8707-5e5b53189e7c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WhenAll(t1, t2)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;.ContinueWith(t =&amp;gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (t.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.Exception; });&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;by&lt;strong&gt; re-throwing&lt;/strong&gt; the exception the unwrapped exception will be &lt;strong&gt;AggregateException&lt;/strong&gt;.     &lt;br /&gt;it may be wise to wrap this pattern in an &lt;strong&gt;extension method&lt;/strong&gt; because it can be a very repetitive one.&lt;/p&gt;  &lt;h5&gt;Proper Disclosure&lt;/h5&gt;  &lt;p&gt;Eran is one of my colleague and definitely one of the more talent Architect I was happened to work with. &lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Async-and-AggregateException-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2013%2F01%2F14%2Fasync-and-aggregateexception.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1660489" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception+handling/default.aspx">exception handling</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.net4.5/default.aspx">.net4.5</category></item><item><title>Tpl Dataflow TOC</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/12/18/tpl-dataflow-toc.aspx</link><pubDate>Tue, 18 Dec 2012 14:03:46 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1566231</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Tpl Dataflow TOC&lt;/h2&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9418_2CB42251.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="TDF, Dataflow, TPL, Task, TOC, ISourceBlock, ITargetBlock" border="0" alt="TDF, Dataflow, TPL, Task, TOC, ISourceBlock, ITargetBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9418_thumb_19169CE3.jpg" width="378" height="291" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/08/02/tpl-dataflow-part-1.aspx" target="_blank"&gt;Part 1&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/09/tpl-dataflow-part-2.aspx" target="_blank"&gt;Part 2&lt;/a&gt;: ITargetBlock&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/10/tpl-dataflow-part-3.aspx" target="_blank"&gt;Part 3&lt;/a&gt;: ISourceBlock&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/16/tpl-dataflow-part-4.aspx" target="_blank"&gt;Part 4&lt;/a&gt;: Block structure&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/28/tpl-dataflow-walkthrough-part-5.aspx" target="_blank"&gt;Part 5&lt;/a&gt;: walkthrough&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1566231" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Dataflow/default.aspx">Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx">TDF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ISourceBlock/default.aspx">ISourceBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ITargetBlock/default.aspx">ITargetBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TOC/default.aspx">TOC</category></item><item><title>Will it crash my process</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/12/10/will-it-crash-my-process.aspx</link><pubDate>Mon, 10 Dec 2012 08:54:52 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1535509</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Will it crash my process &lt;/h2&gt;  &lt;p&gt;here is a short question,   &lt;br /&gt;&lt;em&gt;it was taken from a real life bug that occurs at one of my customers&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;will the following code crash the process?&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6f7136dd-8db6-47b1-b8b9-5573b47099b2" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;List&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;object&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; items = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;List&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;object&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Parallel&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.For(0, 10000000, t =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;items.Add(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it is a non thread safe code that should throw an IndexOutOfRangeException.&lt;/p&gt;  &lt;p&gt;but it wouldn&amp;#39;t crash the process,    &lt;br /&gt;&lt;strong&gt;but what about the following code?&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ed5d934b-0de4-4034-a85d-92dbb2126dd5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;List&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;object&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt; items = &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;List&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;object&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;gt;();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Parallel&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.For(0, 10000000, t =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;items.Add(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;items.Add(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;?&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;);&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_3706_5002_42D83C3E.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Exception, .NET. Parallel. Thread, safe" border="0" alt="Exception, .NET. Parallel. Thread, safe" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_3706_5002_thumb_3E9E3EAC.jpg" width="454" height="318" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;despite the fact that the last addition is seem totally safe,    &lt;br /&gt;it will occasion crash the process.    &lt;br /&gt;&lt;/p&gt;  &lt;h5&gt;so why does it happens?&lt;/h5&gt;  &lt;p&gt;the parallel code part was putting the &lt;strong&gt;list&lt;/strong&gt; itself in a &lt;strong&gt;corrupted state&lt;/strong&gt;,    &lt;br /&gt;which mean that &lt;strong&gt;further access&lt;/strong&gt; will result in an exception.&lt;/p&gt;  &lt;p&gt;the problem is that &lt;strong&gt;further access&lt;/strong&gt; may be locate in a different code file, ant this will make this bug a very illusive one.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1535509" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception+handling/default.aspx">exception handling</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET.+Parallel.+Thread/default.aspx">.NET. Parallel. Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/safe/default.aspx">safe</category></item><item><title>Async - Handling multiple Exceptions</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/12/10/async-handling-multiple-exceptions.aspx</link><pubDate>Mon, 10 Dec 2012 06:30:25 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1535142</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Async - Handling multiple Exceptions&lt;/h2&gt;  &lt;p&gt;the &lt;strong&gt;SDP conference&lt;/strong&gt; was ended a few week ago and I finally find a time to write some comments.&lt;/p&gt;  &lt;p&gt;It was a very successful conference, the feedback and evaluations, of most sessions scored higher than 4.5, some well-over it. For example, &lt;b&gt;one workshop had a perfect 5 / 5 score&lt;/b&gt;, and two other workshops scored 4.92 / 5 and 4.9 / 5. The highest score for a breakout session was 9.39 / 10, which is the highest score we’ve seen to date. my score was &lt;strong&gt;4.90 / 5&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;but this post isn&amp;#39;t about scores, it is about fixing a sample that I was showing at the SDP about &lt;strong&gt;exception handling&lt;/strong&gt; within the &lt;strong&gt;async / await context&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6260_21812378.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Parallel,Task, async, await, exception, whenall, parent, child, .NET 4.5" border="0" alt="Parallel,Task, async, await, exception, whenall, parent, child, .NET 4.5" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6260_thumb_697F2F94.jpg" width="407" height="305" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;when you do use the great &lt;strong&gt;async / await&lt;/strong&gt; syntax you may run into a problem while trying to &lt;strong&gt;handle multiple exception&lt;/strong&gt; which was thrown from async operations (it can happens in cases like &lt;strong&gt;parent child&lt;/strong&gt; relationship or when using &lt;strong&gt;WhenAll&lt;/strong&gt; API).&lt;/p&gt;  &lt;p&gt;by &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;design&lt;/a&gt; the &lt;strong&gt;catch&lt;/strong&gt; closure will handle the &lt;strong&gt;first exception&lt;/strong&gt; and ignore all the other, you can read about it in &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;the following code will catch a single NullReferenceException or ArgumentException exception (the AggregateException will be ignored):&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9ec1c3e9-954e-4314-bc4d-dd63d2a182ba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#008000;"&gt;// this catch will never be target&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Exception&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(&lt;/span&gt;&lt;span style="background:#ffffff;color:#a31515;"&gt;&amp;quot;## {0} ##&amp;quot;&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;, ex.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;if you do want to &lt;strong&gt;catch&lt;/strong&gt; the &lt;strong&gt;AggregateException&lt;/strong&gt; you can use the following work around :&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:96d7cb18-c4f6-4473-9fda-a0ed9b064f0a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t = &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception; });&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; exc &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex.Flatten().InnerExceptions)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(exc.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the difference is at line 12, I&amp;#39;m using a &lt;strong&gt;continuation that re-throw the AggregateException&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;because this is a fairly common practice you may better wrap it within an &lt;strong&gt;extension method&lt;/strong&gt; like the following one:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:04f52111-060f-44c2-abe2-010a5f60b646" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ThrowMultiple(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; { &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;) &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception; });&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; ThrowMultiple&amp;lt;T&amp;gt;(&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&amp;lt;T&amp;gt; t)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; t.ContinueWith(tsk =&amp;gt; &lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (tsk.Exception != &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;null&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Exception;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; tsk.Result;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;});&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;you can use it as follow:&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ef7796b8-6397-45bf-9547-e77705d96328" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;await&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;NullReferenceException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Task&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.Factory.StartNew(() =&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="background:#ffffff;color:#000000;"&gt;{ &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;throw&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; &lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;ArgumentException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;(); },&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.AttachedToParent);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;}).ThrowMultiple();&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;catch&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;AggregateException&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;foreach&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; (&lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;var&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; exc &lt;/span&gt;&lt;span style="background:#ffffff;color:#0000ff;"&gt;in&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt; ex.Flatten().InnerExceptions)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;{&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="background:#ffffff;color:#000000;"&gt;&lt;/span&gt;&lt;span style="background:#ffffff;color:#2b91af;"&gt;Console&lt;/span&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;.WriteLine(exc.GetType().Name);&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="background:#ffffff;color:#000000;"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/p&gt;        &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;catching a single exception within an async method is a &lt;a href="http://blogs.msdn.com/b/pfxteam/archive/2011/09/28/10217876.aspx" target="_blank"&gt;design decision&lt;/a&gt;,    &lt;br /&gt;but you may want to override this decision in some scenarios.&lt;/p&gt;  &lt;p&gt;I hope that in a future release of .NET framework Microsoft consider to &lt;strong&gt;throw an AggregateException&lt;/strong&gt; when the compiler will detect that the developer was adding an &lt;strong&gt;AggregateException catch block&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;in the meantime you can use the extension methods.&lt;/p&gt;  &lt;p&gt;you should not forget to &lt;strong&gt;catch a non AggregateException&lt;/strong&gt; if you have any &lt;strong&gt;code before the await&lt;/strong&gt; (this code will run synchronously).&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1535142" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/exception/default.aspx">exception</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/parent/default.aspx">parent</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/whenall/default.aspx">whenall</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/child/default.aspx">child</category></item><item><title>SDP 2012 - Day 2</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/11/19/sdp-2012-day-2.aspx</link><pubDate>Mon, 19 Nov 2012 16:31:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1458351</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;SDP 2012 - Day 2&lt;/h2&gt;  &lt;p&gt;the &lt;a href="http://sela.co.il/s/sdp2012_july/index.html"&gt;SDP 2012 conference&lt;/a&gt; has day 2 had completed.&lt;/p&gt;  &lt;p&gt;I was speaking about&lt;strong&gt; async / await&lt;/strong&gt;, &lt;strong&gt;Rx&lt;/strong&gt; and &lt;strong&gt;TPL Dataflow&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;you can find the materials for my lecture in &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%215689"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_7858_72285BCB.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="Rx, Tdf, async, await, parallel, TPL" border="0" alt="Rx, Tdf, async, await, parallel, TPL" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_7858_thumb_7C990A1E.jpg" width="484" height="317" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I will give the same lecture on Wednesday next week,    &lt;br /&gt;there is a chance that some place is still available,     &lt;br /&gt;so if you are interesting, you can check it with &lt;a href="http://sela.co.il/s/sdp2012_july/Contact.html" target="_blank"&gt;Sela Marketing&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1458351" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Rx/default.aspx">Rx</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx">TDF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Reactive+Extension/default.aspx">Reactive Extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category></item><item><title>SDP 2012 - Day 1</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/11/18/sdp-2012-day-1.aspx</link><pubDate>Sun, 18 Nov 2012 21:21:40 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1455452</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;SDP 2012 - Day 1&lt;/h2&gt;  &lt;p&gt;the &lt;a href="http://sela.co.il/s/sdp2012_july/index.html" target="_blank"&gt;SDP 2012 conference&lt;/a&gt; has started today.&lt;/p&gt;  &lt;p&gt;I was speaking about what&amp;#39;s new in &lt;strong&gt;.NET 4.5 parallelism&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;you can find the materials for my lecture in &lt;a href="https://skydrive.live.com/?cid=9bf7c1a515d76a9a&amp;amp;id=9BF7C1A515D76A9A%215689" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6811_7822_6E3E272A.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="sdp, parallel, .NET 4.5, Task, Async, await, Sela" border="0" alt="sdp, parallel, .NET 4.5, Task, Async, await, Sela" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6811_7822_thumb_1216D8B8.jpg" width="463" height="320" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;tomorrow I&amp;#39;m giving a full day tutorial about &lt;strong&gt;async / await, Rx and TPL Dataflow&lt;/strong&gt;.&lt;/p&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1455452" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category></item><item><title>My session at the SDP</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/03/13/my-session-at-the-sdp.aspx</link><pubDate>Tue, 13 Mar 2012 12:15:35 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1032426</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Sela conferences (SDP) is coming near (25-29/3/2012).&lt;/p&gt;  &lt;p&gt;you can check the following link for all the SDP&amp;#39;s tutorials and sessions &lt;a title="http://www.sela.co.il/s/SDP2012/index.html" href="http://www.sela.co.il/s/SDP2012/index.html"&gt;http://www.sela.co.il/s/SDP2012/index.html&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I will present &lt;strong&gt;2 full-day tutorials&lt;/strong&gt; (Yaniv Rodenski will join me on the first one).&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;first day &lt;/strong&gt;&lt;a href="http://www.sela.co.il/s/SDP2012/tutorials2.html#v8"&gt;Introduction to the Task Parallel Library&lt;/a&gt; is a full &lt;strong&gt;TPL 4 day&lt;/strong&gt; which will give you a solid knowledge, HowTo and Consideration about building parallel application using the &lt;strong&gt;TPL&lt;/strong&gt; library.&amp;#160;&amp;#160; &lt;br /&gt;at the end of this day we will survey some improvement made in .NET 4.5 to the existing TPL Library.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;second day &lt;/strong&gt;&lt;a href="http://www.sela.co.il/s/SDP2012/tutorials2.html#v12"&gt;Parallel Programming: One Step Beyond &lt;/a&gt;assume that the audience are &lt;strong&gt;already familiar with TPL &lt;/strong&gt;(or participate in the first day).&amp;#160; &lt;br /&gt;this day will focus on 3 subject:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The new &lt;strong&gt;async/await&lt;/strong&gt; keywords (.&lt;strong&gt;NET 4.5 / C# 5&lt;/strong&gt;)&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;TPL Dataflow&lt;/strong&gt; (which is new level of abstraction above the TPL - also &lt;strong&gt;.NET 4.5 / C# 5&lt;/strong&gt;)&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Rx (Reactive Extension)&lt;/strong&gt; which is a great Library for &lt;strong&gt;parallel composition over LINQ&lt;/strong&gt;.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1032426" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Rx/default.aspx">Rx</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx">TDF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Reactive+Extension/default.aspx">Reactive Extension</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SDP/default.aspx">SDP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/.NET+4.5/default.aspx">.NET 4.5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category></item><item><title>Parallel.ForEach behavior</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/02/27/parallel-foreach-behavior.aspx</link><pubDate>Mon, 27 Feb 2012 16:48:57 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1021251</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Parallel.ForEach behavior &lt;/h2&gt;  &lt;p&gt;this post is a direct continuation for the previous &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/02/26/real-life-story-blocking-collection.aspx" target="_blank"&gt;post&lt;/a&gt; about &amp;quot;&lt;strong&gt;Real-life story: Blocking Collection&lt;/strong&gt;&amp;quot;.&lt;/p&gt;  &lt;p&gt;&lt;font color="#666666"&gt;(Real-life story: Blocking Collection).ContinueWith (t =&amp;gt; this post);     &lt;br /&gt;or       &lt;br /&gt;await (Real-life story: Blocking Collection);      &lt;br /&gt;this post;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/Barkan1_60FF5251.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title=":DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection" border="0" alt=":DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection" src="http://blogs.microsoft.co.il/blogs/bnaya/Barkan1_thumb_5602256F.png" width="509" height="435" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;my colleague &lt;strong&gt;Bram Veldhoen&lt;/strong&gt; has suggest to demonstrate the behavior of the &lt;strong&gt;Parallel.ForEach&lt;/strong&gt; &lt;strong&gt;thread&amp;#39;s hunger&lt;/strong&gt; in more pure fashion which doesn&amp;#39;t include &lt;strong&gt;BlockingCollection&amp;lt;T&amp;gt;&lt;/strong&gt; or any other high level Enumerable.&lt;/p&gt;  &lt;p&gt;the following code demonstrate the issue by using a slow Enumerable:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:75fa251c-a82b-4cfd-b4d2-609e3d7e67d1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; _getInt = 0;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt; GetInts()&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Thread&lt;/span&gt;.Sleep(30 * 5000);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;yield&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Interlocked&lt;/span&gt;.Increment(&lt;span style="color:#0000ff;"&gt;ref&lt;/span&gt; _getInt);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Timer&lt;/span&gt; _tmr;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Main()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    _tmr = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Timer&lt;/span&gt;(state =&amp;gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; p = &lt;span style="color:#2b91af;"&gt;Process&lt;/span&gt;.GetCurrentProcess();&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; workerThreads, ioThreads;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#2b91af;"&gt;ThreadPool&lt;/span&gt;.GetAvailableThreads(&lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; workerThreads, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; ioThreads);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;Thread Count = {0}, Available ThreadPool Threads = {1}&amp;quot;&lt;/span&gt;,&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                p.Threads.Count, workerThreads);&lt;/li&gt; &lt;li&gt;        }, &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;, 0, 1000);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Parallel&lt;/span&gt;.ForEach(GetInts(), i =&amp;gt; { });&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.ReadKey();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;when running this code we can see the following output:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/pool_2B45F8F8.jpg"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title=":DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection" border="0" alt=":DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection" src="http://blogs.microsoft.co.il/blogs/bnaya/pool_thumb_7B97B49E.jpg" width="535" height="455" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;we can see that thread count trend is constantly raising while the available ThreadPool&amp;#39;s worker threads is declining.&lt;/p&gt;  &lt;p&gt;you can read the previous &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/02/26/real-life-story-blocking-collection.aspx" target="_blank"&gt;post&lt;/a&gt; for solution to this issue.&lt;/p&gt;   Technorati Tags: &lt;a href="http://technorati.com/tags/SELA" rel="tag"&gt;SELA&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Parallel" rel="tag"&gt;Parallel&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Task" rel="tag"&gt;Task&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Thread" rel="tag"&gt;Thread&lt;/a&gt;,&lt;a href="http://technorati.com/tags/collection" rel="tag"&gt;collection&lt;/a&gt;  &lt;br /&gt;   Windows Live Tags: &lt;a href="http://windows.live.com/connect/tag/SELA" rel="clubhouseTag"&gt;SELA&lt;/a&gt;,&lt;a href="http://windows.live.com/connect/tag/Parallel" rel="clubhouseTag"&gt;Parallel&lt;/a&gt;,&lt;a href="http://windows.live.com/connect/tag/Task" rel="clubhouseTag"&gt;Task&lt;/a&gt;,&lt;a href="http://windows.live.com/connect/tag/Thread" rel="clubhouseTag"&gt;Thread&lt;/a&gt;,&lt;a href="http://windows.live.com/connect/tag/collection" rel="clubhouseTag"&gt;collection&lt;/a&gt;  &lt;br /&gt;     WordPress Tags: &lt;a href="http://wordpress.com/tag/SELA" rel="Tag"&gt;SELA&lt;/a&gt;,&lt;a href="http://wordpress.com/tag/Parallel" rel="Tag"&gt;Parallel&lt;/a&gt;,&lt;a href="http://wordpress.com/tag/Task" rel="Tag"&gt;Task&lt;/a&gt;,&lt;a href="http://wordpress.com/tag/Thread" rel="Tag"&gt;Thread&lt;/a&gt;,&lt;a href="http://wordpress.com/tag/collection" rel="Tag"&gt;collection&lt;/a&gt;  &lt;br /&gt;     Blogger Labels: &lt;a href="http://Bnaya.blogspot.com/search/label/SELA" rel="Tag"&gt;SELA&lt;/a&gt;,&lt;a href="http://Bnaya.blogspot.com/search/label/Parallel" rel="Tag"&gt;Parallel&lt;/a&gt;,&lt;a href="http://Bnaya.blogspot.com/search/label/Task" rel="Tag"&gt;Task&lt;/a&gt;,&lt;a href="http://Bnaya.blogspot.com/search/label/Thread" rel="Tag"&gt;Thread&lt;/a&gt;,&lt;a href="http://Bnaya.blogspot.com/search/label/collection" rel="Tag"&gt;collection&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1021251" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/paralle.foreach/default.aspx">paralle.foreach</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/pool/default.aspx">pool</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Blocking+collection/default.aspx">Blocking collection</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/_3A00_DEV/default.aspx">:DEV</category></item><item><title>Real-life story: Blocking Collection</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/02/26/real-life-story-blocking-collection.aspx</link><pubDate>Mon, 27 Feb 2012 00:00:00 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1020553</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Real-life story: Blocking Collection&lt;/h2&gt;  &lt;p&gt;this post will discuss a real-life story which uncover none trivial (yet logical) behavior which related to &lt;strong&gt;Parallel.ForEach&lt;/strong&gt; and &lt;strong&gt;BlockingCollection&amp;lt;T&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;I will explain why it happens and what how can we handle it right.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9796_2212_15430E41.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Blocking collection, paralle.foreach, tpl, tap, thread, pool, async" border="0" alt="Blocking collection, paralle.foreach, tpl, tap, thread, pool, async" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_9796_2212_thumb_108CB7ED.png" width="462" height="327" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;it all start when &lt;strong&gt;Guy Eden&lt;/strong&gt; from &lt;strong&gt;ITG&lt;/strong&gt; has found that the following code &lt;strong&gt;seem to leak memory&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a387c652-9956-4ddb-8d70-703698016373" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Main()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; bc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;Parallel&lt;/span&gt;.ForEach(bc.GetConsumingEnumerable(), i =&amp;gt; { }));&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.ReadKey();&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it was leaking even those the blocking collection were empty (nothing was adding item to the collection).&lt;/p&gt;  &lt;p&gt;while monitoring this code we can see a steep memory curve constantly raising up to the sky.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_178BA8CB.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Blocking collection, paralle.foreach, tpl, tap, thread, pool, async" border="0" alt="Blocking collection, paralle.foreach, tpl, tap, thread, pool, async" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_202C0157.png" width="532" height="179" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I found it very surprising&amp;#160; because it seem quit reasonable scenario and &lt;strong&gt;BlockingCollection&amp;lt;T&amp;gt;&lt;/strong&gt; is a prime member of the new &lt;strong&gt;TPL&lt;/strong&gt; concurrent collection family.&lt;/p&gt;  &lt;p&gt;so I have decided to ask &lt;a href="http://blogs.msdn.com/b/toub/" target="_blank"&gt;Stephen Toub&lt;/a&gt; about this behavior and as always &lt;strong&gt;Stephen&lt;/strong&gt; has enlighten me about what is really happening in this scenario.&lt;/p&gt;  &lt;p&gt;the following is a snippet from &lt;strong&gt;Stephen&lt;/strong&gt;&amp;#39;s response:&lt;/p&gt;  &lt;p&gt;&amp;quot;The &lt;strong&gt;Parallel.ForEach&lt;/strong&gt; uses the &lt;strong&gt;ThreadPool&lt;/strong&gt;.&amp;#160; ForEach doesn’t use a fixed number of threads, but instead will use whatever threads the pool will make available to it.&amp;#160; And the &lt;strong&gt;ThreadPool has a starvation mechanism&lt;/strong&gt;, which will introduce additional threads if all threads in the pool are blocked and not making forward progress.&amp;quot;&lt;/p&gt;  &lt;p&gt;there are couple of options to cope with this issue:&lt;/p&gt;  &lt;p&gt;the first is fairly simple which is to use the &lt;strong&gt;MaxDegreeOfParallelism&lt;/strong&gt; as shown in the following snippet:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5b84ef05-2cea-4a3d-9ce1-0833e3b2eb45" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; bc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;ParallelOptions&lt;/span&gt; options = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ParallelOptions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                MaxDegreeOfParallelism = 30&lt;/li&gt; &lt;li&gt;            };&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Parallel&lt;/span&gt;.ForEach(bc.GetConsumingEnumerable(), options, i =&amp;gt; { });&lt;/li&gt; &lt;li&gt;    });&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;but this solution is suffering from &lt;strong&gt;inefficient&lt;/strong&gt; use of threads because we are obviously targeting a &lt;strong&gt;long running&lt;/strong&gt; scenario where it is &lt;strong&gt;better not to consume&lt;/strong&gt; the thread from the &lt;strong&gt;ThreadPool&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the other option is to use a custom &lt;strong&gt;scheduler&lt;/strong&gt; (actually this is one of the few scenario where custom scheduler seem right).&lt;/p&gt;  &lt;p&gt;the following snippet show a simplify version of the custom scheduler:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2665a3a6-eb15-4a92-a1fa-a75651382597" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;sealed&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;LongRunningTaskScheduler&lt;/span&gt;:&lt;span style="color:#2b91af;"&gt;TaskScheduler&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;IDisposable&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ConcurrentQueue&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;gt; _requests = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ConcurrentQueue&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CancellationTokenSource&lt;/span&gt; _cancellation = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CancellationTokenSource&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ManualResetEventSlim&lt;/span&gt; _sync = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ManualResetEventSlim&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; _counter = 0;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; LongRunningTaskScheduler(&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; maxDegreeOfParallism)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; maxDegreeOfParallism; i++)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(Execute, &lt;span style="color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;.LongRunning);&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Execute()&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    { &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; cancelToken = _cancellation.Token;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;while&lt;/span&gt; (!cancelToken.IsCancellationRequested)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            _sync.Wait(cancelToken);&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; t;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#0000ff;"&gt;while&lt;/span&gt;(_requests.TryDequeue(&lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; t))&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.TryExecuteTask(t);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            _sync.Reset();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; QueueTask(&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; task)&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    {&lt;/li&gt; &lt;li&gt;        _requests.Enqueue(task);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        _sync.Set();&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; c = &lt;span style="color:#2b91af;"&gt;Interlocked&lt;/span&gt;.Increment(&lt;span style="color:#0000ff;"&gt;ref&lt;/span&gt; _counter);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.Write(&lt;span style="color:#a31515;"&gt;&amp;quot;{0}, &amp;quot;&lt;/span&gt;, c);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; TryExecuteTaskInline(&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt; task, &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; taskWasPreviouslyQueued)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;&amp;gt; GetScheduledTasks()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _requests.ToArray();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;void&lt;/span&gt; Dispose()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        _cancellation.Cancel();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the scheduler is getting &lt;strong&gt;maxDegreeOfParallelism&lt;/strong&gt; as &lt;strong&gt;ctor parameter&lt;/strong&gt; (better name may be poolSize) and construct a pool of long running tasks.&lt;/p&gt;  &lt;p&gt;the tasks will be synchronized using &lt;strong&gt;ManualResetEventSlim&lt;/strong&gt; (&lt;font color="#666666"&gt;which accept cancellation token as parameter to the Wait API AutomaticResetEvent does not have a slim version and does not accept cancellation token&lt;/font&gt;) (see lines 4,5,20).&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;scheduler&lt;/strong&gt; will execute the tasks on a non ThreadPool thread, therefore won&amp;#39;t interfere the &lt;strong&gt;ThreadPool heuristics&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the following snippet show how to use this scheduler:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:be56e00e-621f-4696-b26f-812f7ca39921" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; bc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BlockingCollection&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;Task&lt;/span&gt;.Factory.StartNew(() =&amp;gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;ParallelOptions&lt;/span&gt; options = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ParallelOptions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            TaskScheduler = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;LongRunningTaskScheduler&lt;/span&gt;(30),&lt;/li&gt; &lt;li&gt;        };&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#2b91af;"&gt;Parallel&lt;/span&gt;.ForEach(bc.GetConsumingEnumerable(), options, i =&amp;gt; { &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color:#a31515;"&gt;&amp;quot;.&amp;quot;&lt;/span&gt;); });&lt;/li&gt; &lt;li&gt;    }, &lt;span style="color:#2b91af;"&gt;TaskCreationOptions&lt;/span&gt;.LongRunning);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;you should be&amp;#160; aware of the &lt;strong&gt;Parallel.ForEach behavior&lt;/strong&gt; which assume &lt;strong&gt;short running actions&lt;/strong&gt; and try to use whatever threads the pool will make available to it.&lt;/p&gt;  &lt;p&gt;in cases that you want to use it for potentially long actions, you better use &lt;strong&gt;scheduler&lt;/strong&gt; that will take off the load to a &lt;strong&gt;non thread pool threads&lt;/strong&gt; or at least use the &lt;strong&gt;MaxDegreeOfParallelism&lt;/strong&gt;.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.microsoft.co.il%2fblogs%2fbnaya%2f"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%253a%252f%252fblogs.microsoft.co.il%252fblogs%252fbnaya%252f" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1020553" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/paralle.foreach/default.aspx">paralle.foreach</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/pool/default.aspx">pool</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Blocking+collection/default.aspx">Blocking collection</category></item><item><title>Tpl Dataflow walkthrough - Part 5</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/28/tpl-dataflow-walkthrough-part-5.aspx</link><pubDate>Sat, 28 Jan 2012 17:04:31 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:1000032</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h2&gt;Tpl Dataflow walkthrough - Part 5&lt;/h2&gt;  &lt;p&gt;this post is a &lt;strong&gt;complete walkthrough&lt;/strong&gt; of a &lt;strong&gt;web crawler&lt;/strong&gt; sample that was build purely by using &lt;strong&gt;Tpl Dataflow&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;it was built on &lt;strong&gt;.NET 4.5 / C# 5&lt;/strong&gt; (on a virtual machine using &lt;strong&gt;VS 11&lt;/strong&gt;).&lt;/p&gt;  &lt;p&gt;I will analyze each part of this sample, both by discussing the &lt;strong&gt;Dataflow&lt;/strong&gt; blocks and the &lt;strong&gt;patterns&lt;/strong&gt; in used.&lt;/p&gt;  &lt;p&gt;the sample code is available in &lt;a href="https://skydrive.live.com/redir.aspx?cid=9bf7c1a515d76a9a&amp;amp;resid=9BF7C1A515D76A9A!1404&amp;amp;parid=9BF7C1A515D76A9A!1321&amp;amp;authkey=!ACW2DtTH6k5CuTk" target="_blank"&gt;here&lt;/a&gt; (it is a VS 11 project).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6257_60FC8823.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" src="http://blogs.microsoft.co.il/blogs/bnaya/IMG_6257_thumb_5309F68E.png" width="405" height="307" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;during the walkthrough you will see the following &lt;strong&gt;Tpl Dataflow &lt;/strong&gt;blocks:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;TransformBlock &lt;/li&gt;    &lt;li&gt;TransformManyBlock &lt;/li&gt;    &lt;li&gt;ActionBlock &lt;/li&gt;    &lt;li&gt;BroadcastBlock &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;you will see how the &lt;strong&gt;aysnc / await&lt;/strong&gt; &lt;strong&gt;signature&lt;/strong&gt; of the &lt;strong&gt;Dataflow&lt;/strong&gt; &lt;strong&gt;blocks&lt;/strong&gt; is better for executing an&lt;strong&gt; IO bound operation&lt;/strong&gt; (without freezing a worker ThreadPool thread).&lt;/p&gt;  &lt;p&gt;I should also mention that this post is part of the &lt;strong&gt;Tpl Dataflow &lt;/strong&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx" target="_blank"&gt;series&lt;/a&gt; which you better read before reading this one.&lt;/p&gt;  &lt;p&gt;&lt;font color="#666666"&gt;Disclamation: the web crawler sample is for educational purpose only (running web crawler application may be forbidden by the low of your country).&lt;/font&gt;&lt;/p&gt;  &lt;h5&gt;The sample topography:&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Tpl Dataflow&lt;/strong&gt; application is usually a collection of &lt;strong&gt;agents&lt;/strong&gt; which is linked together in order to compose a complete solution. &lt;strong&gt;each agent is having its own responsibilities and concerns&lt;/strong&gt;. the following diagram present the agent topography for this sample:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_7521B67A.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_27E19722.png" width="520" height="405" /&gt;&lt;/a&gt;&amp;#160; &lt;/p&gt;  &lt;h5&gt;agents block type and responsibilities&lt;/h5&gt;  &lt;p&gt;&lt;strong&gt;Downloader&lt;/strong&gt;: the responsibility of the downloader is to download the html of a web page. it is using a &lt;strong&gt;TransformBlock&amp;lt;Tin, Tout&amp;gt; &lt;/strong&gt;which belong to the executer block family. the transform block is getting a url as the input message and it produce the page&amp;#39;s html as it output.&lt;/p&gt;  &lt;p&gt;the transform block is construct from:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;input buffer (for url) &lt;/li&gt;    &lt;li&gt;task (do the transformation) &lt;/li&gt;    &lt;li&gt;output buffer (for the downloaded html) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;the &lt;strong&gt;task&lt;/strong&gt; is taking one message at a time from the input buffer, transform the message by a&lt;strong&gt; Func&amp;lt;Tin, Tout&amp;gt;&lt;/strong&gt; delegate which it get as a constructor parameter and put the result in the output buffer, where it is available for other blocks to consume.&lt;/p&gt;  &lt;p&gt;later we will see that our crawler transformation is actually taking &lt;strong&gt;Func&amp;lt;Tin, Task&amp;lt;Tout&amp;gt;&amp;gt; &lt;/strong&gt;which is a better signature for &lt;strong&gt;IO bound&lt;/strong&gt; operations (I will discuss it latter).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_5B79DDB3.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_51D16F7D.png" width="400" height="120" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the transform block is a propagator block which mean that it exposed both as a target and a source block. it is implementing &lt;strong&gt;IPropagatorBlock&amp;lt;Tin, Tout&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the following snippet show that &lt;strong&gt;IPropagatorBlock&lt;/strong&gt; is simply an encapsulation of &lt;strong&gt;ITargetBlock&lt;/strong&gt; and &lt;strong&gt;ISourceBlock&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:422eacdf-bea3-4f32-8922-a99ef267161e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IPropagatorBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; TInput, &lt;span style="color:#0000ff;"&gt;out&lt;/span&gt; TOutput&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    : ITargetBlock&amp;lt;TInput&amp;gt;, &lt;/li&gt; &lt;li&gt;      ISourceBlock&amp;lt;TOutput&amp;gt;, &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;      IDataflowBlock&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;Start crawling&lt;/h5&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b05be473-d938-4ab3-995b-326a15e7dd7d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; downloader = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TransformBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; (url) =&amp;gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#008000;"&gt;// using IOCP the thread pool worker thread does return to the pool&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#2b91af;"&gt;WebClient&lt;/span&gt; wc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;WebClient&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; result = &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; wc.DownloadStringTaskAsync(url);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; result;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    }, downloaderOptions);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;as I was mentioning earlier the downloader contractor is getting &lt;strong&gt;Func&amp;lt;Tin, Task&amp;lt;Tout&amp;gt;&amp;gt;&lt;/strong&gt;, therefore we can apply an async lambda expression (line 2). the code &lt;strong&gt;await&lt;/strong&gt; for downloading (at line 6). &lt;/p&gt;  &lt;p&gt;if you are not familiar with the &lt;strong&gt;async / await&lt;/strong&gt; concept you can read &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/29/the-concept-of-async-await.aspx" target="_blank"&gt;this&lt;/a&gt; post or more posts in &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx" target="_blank"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;anyway &lt;strong&gt;while&lt;/strong&gt; &lt;strong&gt;awaiting&lt;/strong&gt; for the download (DownloadStringTaskAsync) the &lt;strong&gt;block&amp;#39;s task&lt;/strong&gt; is actually &lt;strong&gt;return its worker thread&lt;/strong&gt; to the ThreadPool and take advantage of the &lt;a href="http://en.wikipedia.org/wiki/Input/output_completion_port" target="_blank"&gt;IOCP&lt;/a&gt; (IO Completion Port), &lt;font color="#666666"&gt;this is an IO bound operation which mean that no CPU resources is needed &lt;/font&gt;&lt;font color="#666666"&gt;while the network card fetching the data from the network&lt;/font&gt;.     &lt;br /&gt;it is important to understand that while the network card is handling the request the &lt;strong&gt;agent&amp;#39;s task does not fetching another message from the buffer&lt;/strong&gt;, the task will be interrupt when the data will be available. &lt;/p&gt;  &lt;h5&gt;analyzing the html &lt;/h5&gt;  &lt;p&gt;the crawler is using 2 agent for analyze the downloaded html:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;link parser&lt;/strong&gt; (which will look for links elements &amp;lt;a href=&amp;quot;...&amp;quot;/&amp;gt;) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;image parser&lt;/strong&gt; (which will look for image elements &amp;lt;image src=&amp;quot;...&amp;quot;/&amp;gt;) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;both agent should be link to the downloader agent.    &lt;br /&gt;the problem is that &lt;strong&gt;linking both agent directly&lt;/strong&gt; to the downloader agent &lt;strong&gt;will result with starvation&lt;/strong&gt; of one of the agent.     &lt;br /&gt;unlike &lt;strong&gt;Rx&lt;/strong&gt; the most blocks forward messages into the first linked target that accept the message, and ignore other linked targets. which mean that the message will be handle by a single agent at a time.&lt;/p&gt;  &lt;p&gt;broadcast behavior can be achieved by using a &lt;strong&gt;BroadcastBlock&amp;lt;T&amp;gt;&lt;/strong&gt; which is part of the pure buffer family.     &lt;br /&gt;the broadcast block is construct from:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;input buffer &lt;/li&gt;    &lt;li&gt;task &lt;/li&gt;    &lt;li&gt;output buffer of single item. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_6A8096C0.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_4BE6A617.png" width="421" height="125" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the task is fetching a message from the input buffer and place it in the output buffer, from the output buffer the message submit to the linked block.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;broadcast block&lt;/strong&gt; is getting a &lt;strong&gt;Func&amp;lt;T,T&amp;gt; delegate &lt;/strong&gt;as a constructor parameter, the idea behind it is &lt;strong&gt;cloning&lt;/strong&gt; (which will enable separation of the messages).     &lt;br /&gt;if you are passing a &lt;strong&gt;reference type message&lt;/strong&gt; to &lt;strong&gt;multiple agents&lt;/strong&gt;, without cloning, changes that made by one agent will be visible to all the other agents.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;broadcast block&lt;/strong&gt; will use the &lt;strong&gt;cloning delegate&lt;/strong&gt; &lt;strong&gt;before sending&lt;/strong&gt; the message to the linked agents.     &lt;br /&gt;the cloning pattern will ensure that only single block is processing a message instance at a time, this will maintain the &lt;strong&gt;message ownership&lt;/strong&gt; and avoid the needs of data synchronization for &lt;strong&gt;thread safety&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the crawler will use the following block definition for broadcasting:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5e9c8188-1ebd-422c-ad5d-b139b99fb104" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; contentBroadcaster = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BroadcastBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(s =&amp;gt; s);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;in our case the html content is a string which is immutable, therefore no real cloning is needed.&lt;/p&gt;  &lt;p&gt;the crawler will link the agents (blocks) to each other after the construction of all the relevant blocks, right now we are focusing on the agents themselves.&lt;/p&gt;  &lt;h5&gt;Link parser&lt;/h5&gt;  &lt;p&gt;the link parser is using the following regular expression in order to fetch all the links (&amp;lt;a href=...&amp;quot;/&amp;gt;) out from the html and extract the link&amp;#39;s url.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6eaa187b-912d-43d1-be12-7baf5275aa9b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;const&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; LINK_REGEX_HREF = &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;#92;&amp;#92;shref=(&amp;#39;|&amp;#92;&amp;#92;&amp;#92;&amp;quot;)?(?&amp;lt;LINK&amp;gt;http&amp;#92;&amp;#92;://.*?(?=&amp;#92;&amp;#92;1)).*&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Regex&lt;/span&gt; _linkRegexHRef = &lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Regex&lt;/span&gt;(LINK_REGEX_HREF);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;unlike the downloader agent which get a single input (url) and &lt;strong&gt;produce a single output &lt;/strong&gt;(html),     &lt;br /&gt;the &lt;strong&gt;link parser produce multiple outputs&lt;/strong&gt; (links) per each input (html).     &lt;br /&gt;you can use the transform block and set the output type to array of links but the &lt;strong&gt;Tpl Dataflow&lt;/strong&gt; is having a better block for this scenario.     &lt;br /&gt;because the processing of each link is independent of other links, it will be better if the transform output buffer will contain flatten links objects rather then a collection of link&amp;#39;s array.&lt;/p&gt;  &lt;p&gt;the crawler is using the &lt;strong&gt;TransformManyBlock&amp;lt;Tin,Tout&amp;gt;&lt;/strong&gt;. this block is similar to the &lt;strong&gt;transform block&lt;/strong&gt; with only one difference, the delegate at the constructor parameter is one of the following delegates:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Func&amp;lt;Tin, IEnumerable&amp;lt;Tout&amp;gt;&amp;gt;&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Func&amp;lt;Tin, Task&amp;lt;IEnumerable&amp;lt;Tout&amp;gt;&amp;gt;&amp;gt;&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;the block task will extract the outputs results and put each of the extracted result, separately, in the output buffer.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_2F893E2A.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_05C5C337.png" width="499" height="121" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;this is the code for the link parser agent:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e4c3ea87-8947-43a6-8b1a-76d3ab8c8455" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; linkParser = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TransformManyBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;       (html) =&amp;gt;&lt;/li&gt; &lt;li&gt;       {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;           &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; output = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li&gt;           &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; links = _linkRegexHRef.Matches(html);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;           &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Match&lt;/span&gt; item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; links)&lt;/li&gt; &lt;li&gt;           {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;               &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; value = item.Groups[&lt;span style="color:#a31515;"&gt;&amp;quot;LINK&amp;quot;&lt;/span&gt;].Value;&lt;/li&gt; &lt;li&gt;               output.Add(value);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;           }&lt;/li&gt; &lt;li&gt;           &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; output;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;       });&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;it is very straight forward, parse each html by using regex and return list of result which the block will extract into the output buffer.&lt;/p&gt;  &lt;h5&gt;Image parser&lt;/h5&gt;  &lt;p&gt;the image parser is quit similar to the link parser.    &lt;br /&gt;the only differences is that it using different regular expression which extract the image&amp;#39;s url.&lt;/p&gt;  &lt;p&gt;the regex part is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b7393f8c-5e66-4da2-bf4c-3f18ff447c2b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;const&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; IMG_REGEX =&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#a31515;"&gt;&amp;quot;&amp;lt;&amp;#92;&amp;#92;s*img [^&amp;#92;&amp;#92;&amp;gt;]*src=(&amp;#39;|&amp;#92;&amp;quot;)?(?&amp;lt;IMG&amp;gt;http&amp;#92;&amp;#92;://.*?(?=&amp;#92;&amp;#92;1)).*&amp;gt;&amp;#92;&amp;#92;s*([^&amp;lt;]+|.*?)?&amp;#92;&amp;#92;s*&amp;lt;/a&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Regex&lt;/span&gt; _imgRegex =&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Regex&lt;/span&gt;(IMG_REGEX);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;and the parser agent code is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6add4233-6be0-4155-b5f2-2a452e5d6204" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; imgParser = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;TransformManyBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;, &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        (html) =&amp;gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; output = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; images = _imgRegex.Matches(html);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            &lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Match&lt;/span&gt; item &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; images)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;                &lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; value = item.Groups[&lt;span style="color:#a31515;"&gt;&amp;quot;IMG&amp;quot;&lt;/span&gt;].Value;&lt;/li&gt; &lt;li&gt;                output.Add(value);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;            }&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; output;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        });&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;h5&gt;writer agent&lt;/h5&gt;  &lt;p&gt;the last operational agent is the writer agent which will download the an image from a url and save it to the local disk.&lt;/p&gt;  &lt;p&gt;the writer is using a simple &lt;strong&gt;action block&lt;/strong&gt;, which is a simple executer block that have an input buffer and a task.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_494D4E8C.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_1B1352D2.png" width="202" height="109" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;the task is fetching messages from the buffer and execute a delegate which is given as constructor parameter.    &lt;br /&gt;the delegate signature can be either &lt;strong&gt;Action&amp;lt;T&amp;gt;&lt;/strong&gt; or &lt;strong&gt;Funk&amp;lt;T, Task&amp;gt;&lt;/strong&gt;. the latter one is great for &lt;strong&gt;IO bound operation&lt;/strong&gt; (from the same reasons discussed earlier when we was looking on the transform block signature).&lt;/p&gt;  &lt;p&gt;because the writer is doing 2 IO bound operations:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;download the image from the web &lt;/li&gt;    &lt;li&gt;write the image to the file system &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;the crawler is using the &lt;strong&gt;Funk&amp;lt;T, Task&amp;gt;&lt;/strong&gt; signature.     &lt;br /&gt;the writer code is:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c4c18393-327f-4ebb-b35a-2dfe2d982f02" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; writer = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ActionBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#0000ff;"&gt;async&lt;/span&gt; url =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af;"&gt;WebClient&lt;/span&gt; wc = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;WebClient&lt;/span&gt;();&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// using IOCP the thread pool worker thread does return to the pool&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff;"&gt;byte&lt;/span&gt;[] buffer = &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; wc.DownloadDataTaskAsync(url);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; fileName = &lt;span style="color:#2b91af;"&gt;Path&lt;/span&gt;.GetFileName(url);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; name = &lt;span style="color:#a31515;"&gt;@&amp;quot;Images&amp;#92;&amp;quot;&lt;/span&gt; + fileName;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;Stream&lt;/span&gt; srm = &lt;span style="color:#2b91af;"&gt;File&lt;/span&gt;.OpenWrite(name))&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;        &lt;span style="color:#0000ff;"&gt;await&lt;/span&gt; srm.WriteAsync(buffer, 0, buffer.Length);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;});&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the first &lt;strong&gt;await&lt;/strong&gt; at line 5, is awaiting until the task will be &lt;strong&gt;interrupt by the network card&lt;/strong&gt;,     &lt;br /&gt;and the second &lt;strong&gt;await&lt;/strong&gt; at line 12, will await until it will be &lt;strong&gt;interrupt by the file system controller&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;you may have been notice that the second &lt;strong&gt;await&lt;/strong&gt; is within a &lt;strong&gt;using block&lt;/strong&gt;, you can read more about this topic at &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/2012/01/15/using-async-await.aspx" target="_blank"&gt;this&lt;/a&gt; post.&lt;/p&gt;  &lt;h5&gt;link it together&lt;/h5&gt;  &lt;p&gt;right now we are having most of our building blocks and it is time to define the data-flow by linking the block to each other.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;downloader&lt;/strong&gt; should be link to the &lt;strong&gt;content broadcaster&lt;/strong&gt; which in tern should be linked both to the &lt;strong&gt;image and link parser&lt;/strong&gt;, the image parser should be linked to the &lt;strong&gt;writer&lt;/strong&gt; and the &lt;strong&gt;link parser&lt;/strong&gt; should be &lt;strong&gt;linked back to the downloader&lt;/strong&gt; (so it can crawl farther).&lt;/p&gt;  &lt;p&gt;but there is one last issue.    &lt;br /&gt;it happens that some web page is having links that is targeting an image. this lead us to more complex linking where the link parser should be linked both to the downloader and having conditional link to the writer for those url that is having an image suffix.     &lt;br /&gt;as we discuss earlier having a direct link from the link parser to both the downloader and the writer will results with starvation of one of those agents.     &lt;br /&gt;we do need a final broadcast block which will handle this distribution task. &lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:dc3b0644-3e5c-4a37-ba0c-1f8cc81b2cdb" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#fff;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;var&lt;/span&gt; linkBroadcaster = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;BroadcastBlock&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt;(s =&amp;gt; s);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the &lt;strong&gt;link parser&lt;/strong&gt; will be linked to the &lt;strong&gt;broadcaster&lt;/strong&gt; and the broadcaster will be liked to both the &lt;strong&gt;downloader&lt;/strong&gt; and the &lt;strong&gt;writer&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;we have spoke of the conditional link from the link parser and the writer, but it will be more effective if the link parser to the downloader will be link only those pages that are most likely having useful data like php, aspx, htm, ext...&lt;/p&gt;  &lt;h5&gt;Filtering linked messages&lt;/h5&gt;  &lt;p&gt;the following predicates will be use in order to filter linked messages:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0ecb60ad-f394-49fc-b8fb-7505f9c7e164" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;StringComparison&lt;/span&gt; comparison = &lt;span style="color:#2b91af;"&gt;StringComparison&lt;/span&gt;.InvariantCultureIgnoreCase;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#2b91af;"&gt;Predicate&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; linkFilter = link =&amp;gt; &lt;/li&gt; &lt;li&gt;    link.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;.aspx&amp;quot;&lt;/span&gt;, comparison) != -1 ||&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    link.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;.php&amp;quot;&lt;/span&gt;, comparison) != -1 ||&lt;/li&gt; &lt;li&gt;    link.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;.htm&amp;quot;&lt;/span&gt;, comparison) != -1 ||&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    link.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;.html&amp;quot;&lt;/span&gt;, comparison) != -1;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;Predicate&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt;&amp;gt; imgFilter = url =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    url.EndsWith(&lt;span style="color:#a31515;"&gt;&amp;quot;.jpg&amp;quot;&lt;/span&gt;, comparison) ||&lt;/li&gt; &lt;li&gt;    url.EndsWith(&lt;span style="color:#a31515;"&gt;&amp;quot;.png&amp;quot;&lt;/span&gt;, comparison) ||&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    url.EndsWith(&lt;span style="color:#a31515;"&gt;&amp;quot;.gif&amp;quot;&lt;/span&gt;, comparison);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;the first predicate (line 2) will filter the downloader agent target and the second (line 7) will filter the link parser result which is targeting the writer agent.&lt;/p&gt;  &lt;h5&gt;compose the data-flow&lt;/h5&gt;  &lt;p&gt;finally we got to the agent composition.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_1C283BF1.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" border="0" alt="TDF, Tpl,Dataflow, ITargerBlock, ISorceBlock, IDataBlobk, Transform, IPropagatorBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_4ADDCEC6.png" width="520" height="407" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:48166a5f-a656-4f62-a6d9-8881c898564e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:&amp;#39;Courier New&amp;#39;, Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#000080;color:#fff;font-family:Verdana, Tahoma, Arial, sans-serif;font-weight:bold;padding:2px 5px;"&gt;Code Snippet&lt;/div&gt; &lt;div style="background:#ddd;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af;"&gt;IDisposable&lt;/span&gt; disposeAll = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;CompositeDisposable&lt;/span&gt;(&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [downloader] to [contentBroadcaster]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    downloader.LinkTo(contentBroadcaster),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [contentBroadcaster] to [imgParser]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    contentBroadcaster.LinkTo(imgParser),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [contentBroadcaster] to [linkParserHRef]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    contentBroadcaster.LinkTo(linkParser),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [linkParser] to [linkBroadcaster]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    linkParser.LinkTo(linkBroadcaster),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// conditional link to from [linkBroadcaster] to [downloader]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    linkBroadcaster.LinkTo(downloader, linkFilter, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [linkBroadcaster] to [writer]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    linkBroadcaster.LinkTo(writer, imgFilter, &lt;span style="color:#0000ff;"&gt;true&lt;/span&gt;),&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#008000;"&gt;// from [imgParser] to [writer]&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    imgParser.LinkTo(writer));&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;each &lt;strong&gt;LinkTo&lt;/strong&gt; operation return a &lt;strong&gt;disposable&lt;/strong&gt; instance which can be use to&lt;strong&gt; dispose the link&lt;/strong&gt; when it no longer needed. the crawler &lt;strong&gt;compose all those disposable&lt;/strong&gt; together into a single disposable called &lt;strong&gt;dispose All&lt;/strong&gt; by using the &lt;strong&gt;CompositeDisposable&lt;/strong&gt; which is part of the &lt;strong&gt;Rx&lt;/strong&gt; library.&lt;/p&gt;  &lt;p&gt;you can see the conditional LinkTo at line 11 and 13.    &lt;br /&gt;is is &lt;strong&gt;very important&lt;/strong&gt; to set the last parameter of the &lt;strong&gt;LinkTo&lt;/strong&gt; to true if you don&amp;#39;t want to dispose the link when the filter doesn&amp;#39;t match the criteria.&lt;/p&gt;  &lt;h5&gt;summary&lt;/h5&gt;  &lt;p&gt;this post was a walkthrough of a web crawler sample.    &lt;br /&gt;the complete sample, which is available in &lt;a href="https://skydrive.live.com/redir.aspx?cid=9bf7c1a515d76a9a&amp;amp;resid=9BF7C1A515D76A9A!1404&amp;amp;parid=9BF7C1A515D76A9A!1321&amp;amp;authkey=!ACW2DtTH6k5CuTk" target="_blank"&gt;here&lt;/a&gt; (VS 11), is also having exception handling, agent termination after x amount of seconds, prevention of processing the same url twice and more. for simplicity the code within this post was a simplified version.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Tpl-Dataflow-walkthrough-Part-5-Bnaya-Eshet"&gt;&lt;img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblogs.microsoft.co.il%2Fblogs%2Fbnaya%2Farchive%2F2012%2F01%2F28%2Ftpl-dataflow-walkthrough-part-5.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=1000032" width="1" height="1"&gt;</description><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/DEV/default.aspx">DEV</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/SELA/default.aspx">SELA</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Parallel/default.aspx">Parallel</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Task/default.aspx">Task</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Thread/default.aspx">Thread</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL/default.aspx">TPL</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Dataflow/default.aspx">Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx">TDF</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/IDataflowBlock/default.aspx">IDataflowBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ISourceBlock/default.aspx">ISourceBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ITargetBlock/default.aspx">ITargetBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/async/default.aspx">async</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/await/default.aspx">await</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TPL+Dataflow/default.aspx">TPL Dataflow</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TAP/default.aspx">TAP</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/C_2300_5/default.aspx">C#5</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/IDataBlobk/default.aspx">IDataBlobk</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ISorceBlock/default.aspx">ISorceBlock</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/Transform/default.aspx">Transform</category><category domain="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ITargerBlock/default.aspx">ITargerBlock</category></item></channel></rss>