Code Bytes #1 – PLINQ Basics

PLINQ /Parallel LINQ is part of the TPL (Task Parallel Library) and it makes your life easier when it comes to multi-core processor programming which is totally different from multithreading which allows more than one thread per process and you have no idea if they will be equally distributed across CPU cores. To use PLINQ your objects have to be in memory. This means you can’t use AsParallel on LINQ to SQL until you bring all your query results over to the local machine. When it comes to running your code in parallel the key to remember is that the AsParallel method is you friend. Every result that gets returned after your first call to AsParallel is always a ParallelQuery object. You can get more theory here. Now go code!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace OliverCode.CodeBytes
    class ParallelLinqCB
        static void Main(string[] args)
            Action action = (int itemFromList) => Console.Write(itemFromList + ",");
            var lst = Enumerable.Range(1, 10);

             //PLINQ will decide on the number of processors to use in this run up to 64 threads (if you have that much)
             lst.AsParallel().Select(i => i * i).ForAll(action);
            //My machine has 4 cores but i only need it to use up to 2 cores so I use the WithDegreeOfParallelism to restrict it
            //Or I could have use all 4 with the statement WithDegreeOfParallelism(4) hereby not letting PLINQ choose for me


Cropped diagram courtesy of MSDN PFX (Parallel Programming Framework)
Code Bytes 1 - PLINQ

Threading with the Thread Pool and BackgroundWorker

Threading with the Thread Pool and BackgroundWorker

threadsToday I want to talk about threads since every programmer works with them and absolutely loves them. Typically, when we create a thread, your code can look like this:

static void Main(string[] args)

static void TraditionalThreadCreation()
// Create threads
Thread thread1 = new Thread(Task);
Thread thread2 = new Thread(Task);

// this guy will do even numbers
// this guy will do odd numbers
//wait for first thread to finish

static void Task(object p)
for (int i = int.Parse(p.ToString()); i <=10; i += 2)

There is nothing wrong with creating threads like the code above. But there is one thing you have to keep in mind. Thread creation and startup has overhead. A typical thread can take up 1MB of your precious memory. Shocked?? Well you are not alone. Continue reading

MVVM (Model View View-Model) For Dummies/Simplified


A few months ago I took the leap from WinForms programming to WPF and quite naturally, I took to it like a duck to water. Well, to be honest I had been developing Silverlight applications since its inception and being that Silverlight is a subset of WPF it required a low learning curve to catch on. However, the concept of Commanding was a bit different in WPF and I soon began to see how much more powerful Commanding in WPF was compared to Silverlight.

One of the areas in which Commanding is exemplary is in the way in which it complements MVVM.  But what is MVVM, and why is it useful? This is the toughest concept (In my opinion) to grasp when it comes to WPF (and Silverlight) programming. Why you ask? Because it is simple and as developers we often like code or concepts that warp our minds, so when we figure it out we can brag to our peers how it only took 2 hours to understand and implement the next BIG thing (No I am not projecting).  On a side not, I have found that everyone one who blogs about MVVM complicate it by adding too much code which just throws you for a loop. Simplicity is the key to all things complicated. So let’s delve into a little theory and we will finish up with some short-to-the-point code.


The purpose of this post is to

a. Give a simple and clear definition of Model View View-Model

b. Provide a clear and simple sample that clearly illustrates MVVM usage


Figure 1.

MVVM - Model View ViewModel

MVVM - Model View ViewModel

Just in case you cannot read the text in the image here it is below:

  1. The View holds a reference to the ViewModel. The View basically displays stuff by Binding to entities in the View Model.
  2. The ViewModel exposes Commands, Notifiable Properties, and Observable Collections to the View. The View Binds to these ViewModel entities/members
  3. The Model is your data and or application objects that move data while applying Application Logic. If you have a Business Layer then you might not need this.

Above is a simple figure that tells you exactly what MVVM is. In my own words, the ViewModel is the most significant in the entire pattern as it is the glue that sits between the View and the Model and binds both of them together. Now let’s explore some code. Continue reading