Archive for February, 2010

Get a email!

February 25, 2010 Leave a comment

Click here to get a email address, something new other than the usual, finally you can get a chance to have an email address with a better name than what you had to bear with using!

Categories: General

ASP.NET 4 and Visual Studio 2010

February 24, 2010 Leave a comment

For those who have yet to figure what’s new in ASP.NET 4 and Visual Studio 2010, you can refer to for the overview of new features available in the .NET 4 Framework

Categories: ASP.NET

.Net 4.0 Parallelism #3: Tasks

February 19, 2010 Leave a comment

The Tasks class is the main class which gives you the ability to start up new async tasks, monitor their status and implement dependencies. The main aim of this post is to introduce you to the various ways you can create a new task. I will explore more advanced task techniques in a later post.

Note that i’m only going to run thru the 4 most basic constructors, there are other overrides which combines the various parameters, but you should be able to derive them based on the examples given.

Tasks is located in the System.Threading.Tasks namespace.

Start up new tasks

There are several ways to start tasks.

Option #1

You can use the default constructor which allows you to run a delegate which does not take in parameters and returns nothing.

The syntax is: public Task StartNew(Action action);

for example Tasks.Factory.StartNew(() => {doSomeWork();});

Option #2

Now, what happens if you want to pass parameters to the task? No worries, there’s an override which allows you to pass an object to the action. Note however that there is only 1 object. reason being if you want to pass more, you can either create an object array or pass in a struct or class.

The syntax is: public Task StartNew(Action(object) action, object state);

for example Tasks.Factory.StartNew((obj) => {doSomeWork(obj);});

var tokenSource2 = new CancellationTokenSource();

CancellationToken ct = tokenSource2.Token;

Option #3

All looks great so far, but what happens if you want to cancel the tasks in one of the tasks, e.g triggering loads of task to find a particular item and cancelling once this item is found.

The syntax is: public Task StartNew(Action action, CancellationToken cancellationToken);

for example

var theToken = new CancellationTokenSource();

var theTokenSource = theToken.Token;

Tasks.Factory.StartNew(() => {

ct.ThrowIfCancellationRequested(); //return if already cancelled


if (itemfound), theTokenSource.Cancel(); //cancel the tasks

}, theTokenSource);

Option #4

And finally the last variant, where you can specify the TaskCreationOptions.

TaskCreationOptions is actually an enumeration. You can find the full info at Its mainly to specify the run behaviour so that the task scheduler can provide the best possible fit for this task.

The syntax is: public Task StartNew(Action action, TaskCreationOptions taskCreationOptions);

for example: Tasks.Factory.StartNew(() => {doSomeWork();}, TaskCreationOptions.LongRunning);

.Net 4.0 Parallelism #2: Parallelism and ASP.NET

February 18, 2010 2 comments

Parallel Programming works great in client apps, but if you were to want to implement it for ASP.NET via IIS, you will have to be extremely careful.


By design, IIS is multi-threaded, and using the framework, you have the .NET framework load balancing the work requests across the worker threads.

ASP.NET Application

By hosting your application in IIS, you are already having alot of parallelism built in for you, so you need an even more compelling scenario before considering parallelism.

The general guideline is, if your server and application fits the following scenario, you can consider introducing parallelism to improve your application responsiveness.


  1. CPU load is moderate to low
  2. IO read/write is moderate to low
  3. Memory usage is moderate to low

i.e make sure server is not overloaded, otherwise adding parallelism will just overload the server further


  1. Uses high cpu (i.e when running the code, a single core is always maxed out)
  2. Uses objects which are thread safe (when in doubt refer to msdn or local help)
  3. Does not have dependencies on objects which are being processed (e.g data item 2 depends on some output of data item 1)


As a developer, your role is very crucial for the success of parallelism. First you must learn and understand the whole concept of parallelism. Secondly you need to understand your code and think of which sections of code is suitable candidates for performing parallelism. Thirdly decompose the problems and implement the changes. Finally and most important of all, profile your code (via perfmon, ant profiler) to make sure that it runs faster than before.

Problem Decomposition

This is the process where you evaluate the code block  and see if you can break it down into smaller chunks, common examples include foreach, and for loops.

There are 2 types of problem decomposition

1. Data Decomposition: This is where you find yourself applying the same operation to a single collection of strongly typed data. (Strongly typed is in bold because parallelism only works for strongly typed collections). One example is a foreach loop iterating thru all the records in a linq query.

so now instead of doing foreach(var item in items) {…} , you can use the parallel.foreach(items, item => {…})

nb: Parallel.ForEach has many overrides, which i will discuss in a further blog post.

2. Task Decomposition: This is where you find yourself running several smaller tasks in a bigger enclosing task and these smaller tasks themselves are independent of each other.

Since these tasks are independent of each other (strictly it does not really matter even if there are dependencies, you just have to manage them well), you can use Task.Factory.StartNew(() =>{…});

.Net 4.0 Parallelism #1: What is it all about?

February 18, 2010 Leave a comment

CPU manufacturers are moving away from increasing clock speed to putting more cores on a cpu, we now need to look at new development methods which makes full use of these multi core processors to improve performance gains, rather than hoping for a faster core to improve application performance.

With Microsoft .NET 4.0 (or 3.5 with the reactive extensions), Parallel Programming is now possible!

The Task Parallel Library and Parallel LINQ aims to introduce developers to parallel programming to make use of these new cpu technologies. Previously it was possible by using threading, but this is unwieldy and hard to use, not to mention the lousy performance if you were to program it incorrectly.

So what exactly does parallel programming mean?

Lets take a classic example of a simple foreach loop

foreach(var data in allProducts) { doSomething(data);}

In pseudocode, it simple means, do something to all products.

So now, imagine you have a single Quad Core server, and imagine you have a total of 10 products. This is what’s going to happen.

CPU #1 doSomething(1), doSomething(2), doSomething(3)….doSomething(10)
CPU #2 [no work]
CPU #3 [no work]
CPU #4 [no work]

What a waste of CPU resources! You are using just a single core and the rest of the cores are technically not helping you with the task processing at all. Imagine the improvements in performance you will get if you are somehow able to use the rest of the cores easily.

With the new parallels library, you are able to do exactly that. by making a simple change in code, you are able to utilize all the cores

CPU #1 doSomething(1), doSomething(2),
CPU #2 manage threads, doSomething(3), doSomething(10)
CPU #3 doSomething(4), doSomething(5)
CPU #4 doSomething(6), doSomething(8), doSomething(9)

so what exactly is the change in code you say, just add a parallel in front!

Parallel.ForEach(var data in allProducts) { doSomething(data);}

For more information, you can look at the PFX Team blog located at