Hi there! Thank you for visiting my blog. My name is Abhijeet Patel.
I mostly go by just "Abhi".
I'm a developer by trade. I live and work in the beautiful Pacific Northwest for a large financial firm where I serve as a Technical lead.
This blog, its content and opinions are my own.
I blog about technology mainly (as and when I get time), specifically development on the .NET stack since that's where my interests and expertise lie.
I'm excited about the brave new world of open source development on the Microsoft stack and am pretty stoked by the prospect of being able to truly "write once, run anywhere" with .NET core.
I can be reached at abhidotnet at gmail.com

I finally had a chance to install VS2010 Beta 1.Full instruction on how to here
VS2010 has undergone a major overhaul since VS 2008, a peek are some of the cool new eye candy can be found here
I've been waiting to get my hands on the TPL/PLINQ stuff and the Beta was definitely worth the wait. In a previous post we talked about using asynchronous actions for performing long running operations.

I was anxious to see how the new TPL stuff would help in achieving the same thing.

Interestingly enough, the TPL API is based on Actions as well, allowing both the generic and non-generic versions to be used for creating a Task(also supports Funcs).

The Thread pool has undergone some major rework, especially the scheduling algorithm is much more smarter now than the current implementation in.NET 3.5

The unit of abstraction is a Task as opposed to a Thread, so let's dive in and see how this looks and feels as opposed to the abstraction we previously built for asynchronous Actions. In the simplest form, the equivalent declaration for creating a Task that can execute an Action asynchronously looks like so:

publicstaticvoid SomeMethod()

{

// do something here....

Console.WriteLine("In some method");

}

staticvoid Main(string[] args)

{

var task = Task.Factory.StartNew(SomeMethod);

task.Wait();

}

You start a task and Wait for it to complete. Lets create a bunch of these tasks to execute on the GetUsers(), GetCountries() and GetLanguages() method from the previous post.

Task[] tasks = new Task[]

{

Task.Factory.StartNew(() => countries = GetCountries()),

Task.Factory.StartNew(() => countries = GetUsers()),

Task.Factory.StartNew(() => countries = GetLanguages())

};

Task.WaitAll(tasks);

This looks fairly similar from a usage persepctive to what we had for executing asynchronous actions

List<Action> actions = new List<Action>()

{

() => countries = GetCountries(),

() => users = GetUsers(),

() => languages = GetLanguages(),

};

actions.ExecAsync();

One obviously noticeable difference in using the Task is that you use the Factory method StartNew to create and execute the task at the point of declaration, and what you get back is a handle to the executing Task. So the Task[] contains a handle to all the executing tasks.
The ExecAsync extension method on the other hand is passed a collection of Actions to execute. I find the idea of being able to create and execute the Task in one statement very useful and terse.

Let's now compare the performance of executing Actions using Tasks v/s executing Actions using The ExecAsync()

publicstaticclass Extensions

{

publicstaticvoid ExecAsync(this IEnumerable<Action> actions)

{

int count = 0;

AutoResetEvent[] events = new AutoResetEvent[actions.Count()];

IAsyncResult[] results = new IAsyncResult[actions.Count()];

for (int i = 0; i < events.Length; i++)

{

events[i] = new AutoResetEvent(false);

}

foreach (var action in actions)

{

int localCount = count;

results[count++] =

action.BeginInvoke((r) =>

{

try

{

if (r.IsCompleted)

{

Action act = r.AsyncState as Action;

act.EndInvoke(results[localCount]);

}

}

finally

{

//set the event regardless of whether there is an exception so that the main thread

The version using Tasks is almost 5 times faster! which suggests that TPL is doing a far superior job under the covers in effectively scheduling and executing the Actions.
TPL has a host of goodness which I've barely started exploring.Among other goodies, is the ability to have parent-child relationships between tasks and support for cancellation using a CancellationToken.
I hope to do more frequent posts especially around TPL since this has piqued my interest.
As always happy coding!

@krlm: Thanks for pointing that out! Seems like I accidentally linked to the console image from an older post -"Performing multiple Asynchronous Actions for long running operations" while migrating to Skydrive.