<?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 : ActionBlock</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/ActionBlock/default.aspx</link><description>Tags: ActionBlock</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Tpl Dataflow (block structure) - Part 4</title><link>http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/12/16/tpl-dataflow-part-4.aspx</link><pubDate>Fri, 16 Dec 2011 16:34:43 GMT</pubDate><guid isPermaLink="false">b5c4f5bc-c09b-4439-a595-91a98c1847df:959109</guid><dc:creator>bnaya</dc:creator><slash:comments>0</slash:comments><description>&lt;h4&gt;Tpl Dataflow (block structure) - Part 4&lt;/h4&gt;  &lt;p&gt;this is the 4th post in the &lt;strong&gt;Tpl&lt;/strong&gt; &lt;strong&gt;dataflow&lt;/strong&gt; series, you can see other post in this series at the &lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/archive/tags/TDF/default.aspx" target="_blank"&gt;TDF&lt;/a&gt; tag.&lt;/p&gt;  &lt;p&gt;this post will discuss a general implementation of built-in &lt;strong&gt;Tpl dataflow block&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;in general built-in blocks fall into 3 categories&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Pure buffering&lt;/strong&gt; blocks:       &lt;br /&gt;present different strategy of buffering and distribution. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Execution&lt;/strong&gt; blocks:       &lt;br /&gt;manipulate the incoming messages. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Grouping&lt;/strong&gt; blocks:       &lt;br /&gt;deal with different strategies for combine messages from multiple sources. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;as a test case I will discuss the most basic target block called &lt;strong&gt;ActionBlock&amp;lt;T&amp;gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;the &lt;strong&gt;action block&lt;/strong&gt; is an execution block which enables the execution of a delegate to perform some action for the datum of each input message.&lt;/p&gt;  &lt;p&gt;the following code demonstrate basic action block:&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:1eaa26a7-fefa-49c8-973f-1c4c6339898d" 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; ab = &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;int&lt;/span&gt;&amp;gt;(i =&amp;gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000;"&gt;/* Do some processing*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;    &lt;span style="color:#2b91af;"&gt;EventLog&lt;/span&gt;.WriteEntry(&lt;span style="color:#a31515;"&gt;&amp;quot;App&amp;quot;&lt;/span&gt;, i.ToString());&lt;/li&gt; &lt;li&gt;});&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;ab.Post(1);&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;ab.Post(2);&lt;/li&gt; &lt;li&gt;ab.Post(3);&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;all the built-in blocks construct from one or more &lt;strong&gt;internal buffer&lt;/strong&gt; for the input message and one or more &lt;strong&gt;tasks&lt;/strong&gt; (&lt;font color="#666666"&gt;by default single task&lt;/font&gt;) which process single message each time.&lt;/p&gt;  &lt;p&gt;action block internal is built form a &lt;strong&gt;single internal buffer&lt;/strong&gt; and a &lt;strong&gt;task&lt;/strong&gt; (&lt;font color="#666666"&gt;by default single task&lt;/font&gt;) which will fetch single a message each time and invoke the delegate with the message datum.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/bnaya/image_039EFB50.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, block, action, ITargetBlock, ISourceBlock" border="0" alt="TDF, TPL, Dataflow, block, action, ITargetBlock, ISourceBlock" src="http://blogs.microsoft.co.il/blogs/bnaya/image_thumb_0F2492C2.png" width="433" height="113" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;it is important to understand that the &lt;strong&gt;dataflow blocks release&lt;/strong&gt; the&lt;strong&gt; internal task&lt;/strong&gt; whenever the &lt;strong&gt;internal buffer become empty&lt;/strong&gt;. then whenever new message reach the internal buffer the block create a new task.&lt;/p&gt;  &lt;p&gt;as I said in previous posts Tpl dataflow were design to deal with immense throughput and it try to avoid over subscription. &lt;/p&gt;  &lt;p&gt;both the idea of using buffering instead of creating task per message and releasing the task whenever it doesn&amp;#39;t needed anymore lead to &lt;strong&gt;better usage of the CPU and memory resources&lt;/strong&gt;.&lt;/p&gt;  &lt;h5&gt;Summary&lt;/h5&gt;  &lt;p&gt;action block is a very simple one. the focus of this post was to present some of the ideas behind the built-in block construction in general.&lt;/p&gt;  &lt;p&gt;it is essential to understand the idea of single task (&lt;font color="#666666"&gt;by default&lt;/font&gt;) over internal buffering, when you start interacting with the blocks. the behavior of the entire blocks ecosystem (&lt;font color="#666666"&gt;when we chain blocks into more complex flow&lt;/font&gt;) will be depend on how each block buffering and execution were configured.&lt;/p&gt;  &lt;p&gt;I will survey different configuration option on latter posts.&lt;/p&gt;  &lt;p&gt;in the next post I will speak on the block life-time management.&lt;/p&gt;  &lt;br /&gt;&lt;a href="http://dotnetshoutout.com/Tpl-Dataflow-Part-4-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%2F2011%2F12%2F16%2Ftpl-dataflow-part-4.aspx" /&gt;&lt;/a&gt;&lt;img src="http://blogs.microsoft.co.il/aggbug.aspx?PostID=959109" 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/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/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/ActionBlock/default.aspx">ActionBlock</category></item></channel></rss>