Home > ASP.NET, Parallel Programming > .Net 4.0 Parallelism #3: Tasks

.Net 4.0 Parallelism #3: Tasks

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 http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcreationoptions(VS.100).aspx. 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);

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: