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 »:

nugettpl

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

dataflowwriteinnuget

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

dataflowinsolexpl

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 :

actionBlock

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:

postactionblock

 

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

wholeexampledataflow1

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 :

dataflowoutputconsole1

 

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.

TransformBlock

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

LinkTodataflow

 

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

postactionblock

 

the whole code :

wholeexampledataflow2

Run it :

dataflowoutputconsole2

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

Advertisements

, , , ,

  1. Poster un commentaire

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s

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

%d blogueurs aiment cette page :