Andy's observations as he continues to attempt to know all that is .NET...

Monday, June 15, 2009

Long or Short running tasks

Been playing around with the new version of Pfx for .NET.  I must say Ive been very impressed with the improvements since the last CTP for .NET 3.5.  So here goes a series of blogs and screen casts on some on various bits of Pfx for .NET 4 BETA 1.

The one thing that really shouts is that the original  Pfx types are no longer something for just  fine grained parallelism its is a unification of the various threading API’s.  Unifying Api’s is something not new to the .NET framework its been happening since 1.1.  As developers its great that whilst the framework is evolving a constant effort is being made to refactor and simplify previous complexity.

So previously If  I wanted to create a short running piece of background work I would favour the thread pool, if I was to write a long running piece of background activity I would have to create my own custom thread.   Creating a short running thread using the thread pool meant either QueueUserWorkItem or Delegate.BeginInvoke, and a long running via new Thread(), and calling Start.

Now for either types of situation  you simply create a new Task using the new Task type, either via the Task Factory ( not a real GOF Factory, but don’t get me started on that ), or via new Task()

Task bgTask = Task.Factory.StartNew( MyShortRunningTask);

For a long running task we use the same API but this time giving it a hint that this is a long running task and shouldn’t therefore use a thread pool thread, but create a new thread outside the thread pool

Task bgTask = Task.Factory.StartNew( MyLongRunningTask, TaskCreationOptions.LongRunning);

So too very similar calls but leaving it up to the framework to decide how best to schedule the work. 

No comments:

About Me

My photo
Im a freelance consultant for .NET based technology. My last real job, was at Cisco System were I was a lead architect for Cisco's identity solutions. I arrived at Cisco via aquisition and prior to that worked in small startups. The startup culture is what appeals to me, and thats why I finally left Cisco after seven years.....I now filll my time through a combination of consultancy and teaching for Developmentor...and working on insane startups that nobody with an ounce of sense would look twice at...