Articles tagués TPL

DataFlow – TPL ( Task Parallel Library) – Asynchronous / Parallel programming using Microsoft

DataFlow allows you to make asynchronous process with different step of the process (called Block).First, download the TPL library using Nuget (and visual studio 2012 if you have it ;-). Create first an application console and after use nuget as follows « Manage Nuget Package »:


Write “dataflow” on the search textbox you will obtain the nuget package as follows:


In Solution Explorer, you will notice the TPL library as follow


The simple block called action block allow you to define a function asynchronously.

To write it is very easy, you have only to instantiate an ActionBlock first :


here we use linq to make a loop ten time on our process which waits one minute and write the string parameter in a console.After declare it, you have only to call it using Post method and pass a string parameter in our case:



the whole code to run your DataFlow in a console application is:


You will notice that the dataflow will occur in the same time as the while loop where we write dot in the output.Run it and enjoys :



waw, this a parallel program 😉

Now we will add another block to make our dataflow process.We create a TransformBlock which take a string as input and return the string in UpperCase.


We link the TransformBlock to the previous ActionBlock to write in console ouput the string in upper case:



Finally, we post a string using the transform block (which transfer the string to the ActionBlock because of  the link we ‘ve made:



the whole code :


Run it :


congratulation, you have made your first dataFlow (using two blocks) in a parallel programming 😉

With TPL, you can associate as many block you want  in asynchronous way


, , , ,

Poster un commentaire

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha