Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TPL TaskFactory.FromAsync vs Tasks with blocking methods

Tags:

I was wondering if there were any performance implications between using TPL TaskFactory.FromAsync and using TaskFactory.StartNew on blocking versions of the methods. I'm writing a TCP server that will support no more than 100 concurrent connections. After writing code with the first option & chaining multiple read & write operations with continue with, I was left with ugly, hard to debug code.

I believe writing code with the synchronous version & then wrapping it with a Task would decrease complexity & increase testability, but I'm worried about the performance implications of doing this.

For example, are there any performance differences between these 2 calls:

NetworkStream stream;
byte[] data;
int bytesRead;

//using FromAsync
Task<int> readChunk = Task<int>.Factory.FromAsync (
      stream.BeginRead, stream.EndRead,
      data, bytesRead, data.Length - bytesRead, null);

//using StartNew with blocking version
Task<int> readChunk2 = Task<int>.Factory.StartNew(() => 
      stream.Read(data, bytesRead, data.Length - bytesRead));
like image 811
Jonathan Matheus Avatar asked Feb 16 '11 16:02

Jonathan Matheus


People also ask

Which method provides a Convenient way to run any number of arbitrary statements concurrently?

Invoke method provides a convenient way to run any number of arbitrary statements concurrently. Just pass in an Action delegate for each item of work. The easiest way to create these delegates is to use lambda expressions. The lambda expression can either call a named method or provide the code inline.

What is Task Parallel Library in c#?

The Task Parallel Library (TPL) is a set of public types and APIs in the System. Threading and System. Threading. Tasks namespaces. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.

How does Task work in c#?

A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result. There is no direct mechanism to return the result from a thread. Task supports cancellation through the use of cancellation tokens.

How to create new Task c#?

To start a task in C#, follow any of the below given ways. Use a delegate to start a task. Task t = new Task(delegate { PrintMessage(); }); t. Start();


1 Answers

You absolutely want to use FromAsync when an API offers a BeginXXX/EndXXX version of a method. The difference is that, in the case of something like Stream or Socket or WebRequest, you'll actually end up using async I/O underneath the covers (e.g. I/O Completion Ports on Windows) which is far more efficient than blocking multiple CPU threads doing a synchronous operation. These methods provide the best way to achieve I/O scalability.

Check out this section of the .NET SDK on MSDN entitled TPL and Traditional .NET Asynchronous Programming for more information on how to combine these two programming models to achieve async nirvana.

like image 165
Drew Marsh Avatar answered Sep 28 '22 16:09

Drew Marsh