November16

Async Data Flow Block in TPL

Check out the new System.Threading.Tasks.Dataflow.dll for processing blocks of data in an asynchronous manner.  I watched this video on Channel 9 and it blew me away.  I hope this goes into .Net 4.5 by default.

Intro to TPL Dataflow

Very easy to control things like a data pump where you want to process the data as it comes into a queue.  You can even let the ActionBlock run in and just have it call you back when it is done.

 

Sample code

// Setup an action block - nothing is happening here 
// until we post something to this block
var ab = new ActionBlock<int>( i =>
{
// Wait without blocking the thread
await TaskEx.Delay(1000);
Console.WriteLine(i);
}
, new DataFlowBlockOptions(TaskScheduler.Default, 4));


// Now post 10 items to the block
for(int i = 0; i < 10; i++)
{
ab.Post(i);
}

// Tell the action block we are done and it should stop
ab.DeclinePermanently();

// Wait for it to done processing
ab.CompletionTask.Wait();

More information

Visual Studio Aync Website

Comments are closed