Vlad's blog

In programming veritas

Posts Tagged ‘concurrency

Introduction to TPL Dataflow

leave a comment »

TPL Dataflow Library is one of underestimated libraries which have not gained as much popularity as other TPL components. I believe the main reason is a fact that Microsoft did not include Dataflow Library in the .NET Framework 4.5, you should install it separately. Anyway in this post I am going to quickly describe main features of TPL Dataflow and answer a question why and when you need to use this library.

TPL Dataflow Library is designed to help programmers write concurrent enabled applications. You may say that this is what TPL is all about. True, but Dataflow Library is supposed to be used in very specific scenarios. Imagine you are developing an application that has to process many request which is typical scenario for server side applications. And processing of a request involves many concurrent tasks: Web API calls, database transactions, file IO operations, image processing etc. Also you want to be able to control resource utilization in order to keep your server responsive. Certainly, you can do everything described above using TPL tasks, develop own queues for message prioritization and use synchronization primitives to protect shared data. TPL Dataflow provides a programming model that hides low level details of managing concurrent workflows and allows you to focus on your business logic.

Programming Model

The main concept in Dataflow is a block. You can consider a block as data structure that can buffer and process data. Normally your Dataflow application is a collection of linked blocks forming a workflow specific for your needs. Actual communication between blocks is organized in a form of messages passed from one block (source) to another (target). There are different types of blocks: data transform block, buffer block, action block. You can build a simple pipeline which is a linear sequence of blocks or network, which is a graph of blocks.

Common blocks

Transform block

Transform block is used to receive input value and return transformed value. In the example below TransformBlock receives user names and returns user photos.

var photoService = new PhotoService();
var transformBlock = new TransformBlock<string, Photo>(async userName =>
 Photo userPhoto = await photoService.GetUserPhoto(userName);

 return userPhoto;

Read the rest of this entry »


Written by vsukhachev

March 5, 2017 at 12:32 pm

Posted in Development

Tagged with ,

async/await in C#

leave a comment »

Concurrency is mandatory part of almost every modern application. In .Net TPL provides solid foundation for organizing different scenarios of asynchronous communication. Starting from MSVS 2012 we have two new keywords to deal with concurrent methods: async and await.

As example consider client server remote communication. The Server maintains a list of jobs that Client must execute. Once client is successfully authorized it starts retrieving jobs one by one. When a job is finished the Client asks the Server to remove this job from the list and retrieves the next job. All communication between Server and Client is asynchronous because the nature of communication protocol. But the logic is definitely sequential:

  • Authorize
  • Retrieve first job
  • Execute first job
  • Finalize first job on the Server
  • Retrieve the next job

We want to update the UI in non blocking fashion. So if authorization takes 3 seconds the UI should be responsible. Below is the implementation of sample Authentication service. It just adds a delay to simulate server side processing.

public class AuthenticationService
   public Task<bool> Authenticate()
     return Task.Delay(TimeSpan.FromSeconds(3)).ContinueWith(t => true);

Read the rest of this entry »

Written by vsukhachev

November 24, 2014 at 1:21 pm

Posted in Development

Tagged with