Skip to main content

Posts

Showing posts from July, 2010

Parallel LINQ (PLINQ) - Intro

.Net 4.0 supports parallel LINQ or PLINQ, PLINQ is a parallel implementation of LINQ. PLINQ has the same characteristics has LINQ, in that it executes queries in a differed manner. However, the main difference is that with PLINQ, your data source gets partitioned and each chunk is processed by different worker threads (taking into account the number of processor cores that you have) , making your query execute much faster in certain occasions. Running a query in parallel is just a matter of calling the AsParallel() method of the data source, this will return a ParallelQuery<T> and your query will execute parallel. Let's look a code sample... var query = from num in source.AsParallel() where num % 3 == 0 select ProcessNumber(num); Now, when this query is iterated our a foreach loop or when you call ToList() etc..the query will be run in different worker threads. Although, you have parallelized your query execut

Implementing Asynchronous Callbacks with Task Parallel Library

Bored again...so thought of posting how you can implement callback with Task Parallel Library (TPL). So what am I talking here, basically I start a task in one thread and I want it to call another method once it completes (Asynchronous callbacks ). Here is a sample code.... Task<int> parent = new Task<int>( () => { Console.WriteLine("In parent"); return 100; } ); Task<int> child = parent.ContinueWith( a => { Console.WriteLine("In Child"); return 19 + a.Result; }); parent.Start() Console.WriteLine(child.Result); The code explains it all, all I have to do is create the task and then call its ContinueWith method and register the callback, its important to note that the parent task is an input to the continuation callback and the result of the parent can be accessed by t

Running Parallel Tasks with The Task Parallel Library

Down at home with Conjunctivitis, was boring at home, so was listening to some old classics and then thought of writing a post on how you can run tasks with the Task Parallel Library (TPL). Going forward, Microsoft encourages developers to use TPL for concurrent programming. In my previous post I talked about data parallelism , where I showed how blocks of work running inside a loop can be scheduled to run on different threads. In previous versions of .Net if I want to execute a task in another thread I had do this. Thread thread = new Thread( () => { //Do some work Console.WriteLine("Starting thread"); } ); thread.Start(); With TPL I only do this.. Parallel.Invoke( () => { //Do some work Console.WriteLine("Starting thread"); }

ConcurrentBag<T> - Thread Safe Collections

.Net 4.0 introduces a new namespace, System.Collections.Concurrent, this namespace contains a set of collections that would be very much useful in threaded programming. The Add or Remove methods of List<T> is not thread safe, meaning that if you are adding or removing items from a list which is accessed from multiple threads, you will end up overwriting some of the items. So, in multi threading programming, you would need to lock the list before adding or removing items from it. The ConcurrentBag<T&gt in the System.Collections.Concurrent is a thread safe collections i.e all you have to do is call Add or Remove in the usual way and the collection will take care of adding the items without overwriting them. Here is an example using the ConcurrentBag<T&gt with the Parallel Task Library... ConcurrentBag array = new ConcurrentBag (); try { Parallel.For(0, 100000, i => { //Do some

Task Parallel Library - Refresher - Stop an Iteration

I have been talking about the Task Parallel Library (TPL) 2 years back , when it was in CTP, I was taking a class on High Performance Computing yesterday, and I just remembered that I have forgotten all about this library :). This library has now been officially released with .Net 4.0 and Microsoft recommend you to actually use this library if possible when writing concurrent programs, so that your program can take maximum advantage on the numbers of processes that you have. I thought of posting a sample code as a refresher,. Here is the example, I have a list of Customer objects and I need to get the object that matches a specific criteria, lets say the Name property should be "F". Here is how my Customer object looks like. public class Customer { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } Lets assume that the Name property is unique. If I was to write the algorithm for this in .Net 1.1 or

HashSet<T> vs .Net 4.0 SortedSet<T>

HashSet<T> has been there for a while, it can store objects in such a way that the time to add an item to the set or remove it or search for an item is O(1), constant time. It uses hash based implementation to achieve this constant time for these operations, however when you want to iterate this collection in a sorted way, the operation is intensive, as the values within the HashSet is not sorted, you need to create a sorted collection and then iterate this collection. As the values are stored within the collection indirectly based on hash, the sort operation is expensive and also a new collection has to be created. This is where SortedSet<T> comes into play, this collection type was introduced in .Net 4.0, when you add item to this collection, the item is placed in the collection according to the sort criteria, thus when you need to iterate this sorted collection, it is much faster then the HashSet. SortedSet has it's own cons, now that the items has to be placed in th

ASP.NET Localization - Implicit Resource Assignment

In one of my previous post I talked about ASP.NET localization basic and 2 ways of which resources can be assigned to ASP.NET pages and controls were also discussed. In this post I will talk about a powerful ASP.net feature that can be used to assign resources implicitly. This feature can be used to assign multiple resources to a control at one shot without individually assigning resources. Lets take an example to demonstrate this, I am going to place a simple button control on a page. Now I want assign 3 types of resources, the Text, Color of the button and the tooltip of the button. This is my resource file. I have assigned values for all fields that I require. Note how the key has been named, that is a prefix followed by a period and then followed by the property of the control that I need to assign these resources too, for an example "button" is my prefix followed by a period and then the "Text" property. Now, in my ASPX page all I have to do is this. Note that

Satellite Assemblies and Strong Names

A good friend of mine from another project was talking about how a satellite assembly for a given culture that he created for an ASP.NET server control project did not get picked up for the that culture and the default resource was picked up. I was so curious to why this did not happen, and it was today that it hit me that there project is signed with a strong key and that he did not sign the satellite assembly with the same key. In other words, if your main project is signed then so does all your satellite assemblies need to be signed with the same key, if not the loading for that assembly will fall and it will resort to the default resource bundle, that is contained within the main assembly.

ASP.NET Localization

I have written some blog post on globalization before, but all this was something to do with desktop application or general best practices. This week I was interested in localizing ASP.NET applications (well, I was forced to :)) So, let me start by giving an introduction to ASP.NET resource assignment features. Basically it's the same .NET concept, you create resource files, compile and create satellite assemblies and link it to the main DLL. The resource assignment would happen through the resource manager. Although you can compile your resources before deploying, you can also take the feature of a ASP.NET folder named App_LocalResources, you can just plaace all your resource files with these folder and ASP.NET will automatically compile and create satellite assemblies. The screen shot on the left shows a screen shot of my solution. Here I have added the resource files for the Local.aspx into the App_LocalResources folder; I have added 3 different resource files, one for the defau