How to do Lambda Expressions: The ForEach clause

April 12, 2008

How many times have you written code such as:

Customer[] customers = Customer.GetAllActiveCustomers();
foreach (Customer c in customers)
  Debug.WriteLine("Name:" + c.Name);
  Debug.WriteLine("Age:" + c.Name);

 Well, using lambda expressions, you can now take a shortcut!

The above code can now be written like this:

var customers = Customer.GetAllActiveCustomers();
customers.ForEach(c =>
  Debug.WriteLine("Name:" + c.Name);
  Debug.WriteLine("Age:" + c.Name);

I don’t know about you, but this seems to be easier to read. It follows object-oriented principles of executing methods directly off objects. No need to add overly obvious repetitive code. The compiler works out the types for you!

The => token is a direct substitute for the delegate clause. When the compiler sees the =>, it says that the preceeding part of the statement is the parameter list of a delegate, and what follows the =>  is the statement to execute.  

Most collection types now contain a number of methods that allow lambda expressions, and therefore delegates also, to be supplied. ForEach is one of these. I hope to see more statements like this in the code I see when I’m out consulting!

Windows Workflow Threading

April 12, 2008

I went to the Hands On Lab Day put on by Victoria .Net today, so thanks go to all the guys and especially Readify’s Mahesh Krishnan for organising the day. I’m a hands-on kind of person, so I learnt heaps of stuff.

I was having a play with the Windows Workflow, building a state machine, and we got on to Parallel processes. Basically, we were trying to determine if a different branch in a Parallel process ran on it’s on thread. The implication was that if it did, then it would work well on multi-processor boxes.

Working with Damian Edwards (also of Readify) we did a few tests. Damo wrote some debugging code inside a Code Activity so that we could see what thread that the process was running on and whether the thread was from the thread pool.

So the answer to the question of whether the different branches can run on different processors is no. Both branches ran on the same thread. This means that a Parallel task only guarantees that it will not continue until all tasks in the various branches are completed.

Then we extended the test program to determine whether different instances of a Workflow run on separate threads.

Our first test appeared to show that instances all ran on the same thread. But the reason that the same thread was used for both instances in the first case was that Windows Workflow utilises the Thread Pool. When the first instance exited, the Workflow engine (which, incidently, also runs in its own thread, and that makes sense) requests the next available thread. In this case, it was the same thread that the first instance had exitted from, and that was because the workflow example we’d created ran too quickly.

So after adding a delay inside the workflow and trying it again, we found that each workflow instance does indeed potentially run on its own thread. Which to me means that Windows Workflow can scale across multiple processors.

Upgrade your notebook hard drive on Windows Vista using a USB drive

April 5, 2008

I just purchased a new 200GB 7200RPM drive for my laptop, to replace a 100GB drive that I had trouble making enough space on (A$195 at CPL). I went through the process of investigating the ways to backup the existing drive and even started to backup the drive using DriveImage XML. The problem is, I wasn’t convinced that with all the extended attributes in Vista, that a third-party image backup was going to do the job. The light-bulb in my head suddenly shone brightly. Surely Windows Vista has an equivalent of NT Backup? And it does.

Called the “Backup and Restore Centre”, it allowed me to make a complete backup of the drive to my NexStar external USB drive. I needed to have enough space equivalent to the amount of space taken up on my disk.

I successfully made the backup, then switched over to the new drive. I thought I had to install un-activated copy of Vista and then run the Restore complete backup from the Backup and Restore Centre. It turns out that you will be unable to restore the image using the Backup and Restore Centre.

I don’t have an emergency partition on my laptop, so I needed a Vista installation DVD. I ran the DVD, selected my language, and then on the next screen was able to select the option “System Repair”. It then gave me some options for what I wanted to do next. At this time, I plugged in my USB drive and selected “Restore a complete backup”. But alas, my image wasn’t on the list of available backups!

It turns out, that if you want to restore from a USB drive, the USB drive needs to be plugged in before you boot the computer. If it’s not, the drive won’t be recognised and the drivers won’t be installed at the right time.

Ok, so I started the process again, this time with the USB drive already plugged in on boot, and this time when it got to the Restore a complete backup screen, my backup image was available, and I was able to restore successfully.

When it restored the image on the new larger sized drive, it was restored to the original size, so I went and right-clicked on Computer in the Start Menu and selected Manage.  This takes you to the Computer Management MMC applet.  I selected Storage and Disk Management, and this enabled me to either expand or create another partition.

I originally expanded the partition, but later on I decided to split the partition in half. The reason? I only have 100GB external drives. So I created two partitions less that 100GB each so that I could choose to backup an individual partition separately from the other.

Next, activation. After restoring the hard drive image to a new drive, you will have to re-activate Windows Vista. I didn’t have the product key handy, so I chose to do an automated telephone activation. This initially failed claiming too many activations, however I got put through to a nice person on the service desk, explained what I was doing, and he provided me with the activation key, which involves keying in about 8 lots of 6 digits.

And whalla, I am now completely up and running, and without having to pay for extra software. I hope this helps someone!