VHD Attach 1.60

Illustration

Since first version of VHD Attach there were requests for auto-mounting of drives upon startup. With this version that feature is finally available. :)

Other change that some will like is possibility to enable/disable context menu items for explorer. Now you can choose whether you want all items there, some of them or none.

Download is available here.

The Devil Is In The Details

In quite few programs I see following pattern:

void OnMyEvent(EventArgs e) {
    if (this.MyEvent != null) {
        this.MyEvent(this, e);
    }
}

I must confess that I use same pattern in quite a few pieces of my own code. Nevertheless, this is wrong.

Issue here is that MyEvent can change between check and call statements. If change consists of adding one more delegate everything is fine. If change is removing all existing delegates you are in trouble.

Here is scenario. First line will check whether MyEvent is different than null (which it is at that point in time) and, exactly at that time, control switches to another thread and makes MyEvent null. Once our thread resumes it will use MyEvent which is now null. Exception!

This issue is highly timing sensitive and it is near impossible to reproduce it. You may have that bug for couple of years and never hit it. But it is there.

There are two distinct paths to remove this problem. One is just putting everything into lock statement. This will fix issue but it will also introduce overhead, performance problems and even potential deadlocks. It all depends on what exactly is done in called method. It is not a pattern I would be comfortable with.

Another path is to exploit fact that event delegates are immutable. Every instance has all data needed and if you copy it to local variable there is no need to worry about it changing value. With simple change we now have correct code:

void OnMyEvent(EventArgs e) {
    var ev = this.MyEvent;
    if (ev != null) {
        ev(this, e);
    }
}

10-4

Illustration

Visual Studio 2010 is here!

MSDN subscribers can start download immediately while others will need to wait a little.

As soon as I download it, fate of Visual Studio 2008 is sealed. So long, and thanks for all the fish.

[2010-04-12: Express editions are available for download now.

[2010-04-12: Trial version of full Visual Studio is also available .

[2010-04-12: Unfortunately there is no Ribbon control for managed code. Big disappointment for me.

WPF in Story of Resources.

As I started to play with WPF and XAML one of first things was to get some free vector images. Those images came already formated as XAML resources:

<ResourceDictionary ...>
    <DrawingImage x:Key="Horizon_Image_Up1">
        ...
    </DrawingImage>
</ResourceDictionary>

All one needed to do was to include them as resource. I personally like to do that at application level:

<Application ...>
    <Application.Resources>
        <ResourceDictionary Source="Resources/Left1.xaml" />
        <ResourceDictionary Source="Resources/Down1.xaml" />
        <ResourceDictionary Source="Resources/Up1.xaml" />
    </Application.Resources>
</Application>

Once we do this there will be error message greeting: “Property ‘Resources’ accepts only one object.”

There are two solutions to this problem. First one is obvious - put all resources in same file. I personally tend to avoid it since I like to reuse resources in multiple projects and I hate carrying bunch of unused images.

Second solution is to merge all those dictionaries into one. Fortunately, there is simple way to do it:

<Application ...>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <Application.Resources>
                <ResourceDictionary Source="Resources/Left1.xaml" />
                <ResourceDictionary Source="Resources/Down1.xaml" />
                <ResourceDictionary Source="Resources/Up1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

With that you have single dictionary consisting of all individual images. Do notice that, if there is same key defined in different files, only image that was defined last will be used.

The Only Winning Move Is Not to Play

Illustration

I own BeBook mini for quite some time now. Once it gets noticed I get quite a few questions. Usually people wonder about eInk screen, supported formats, battery life… Lately question that comes quite often is “Are you sorry?”

They point that there are bigger devices, better screens, shinier features… New e-book readers come out almost every month.

With current progress in technology, I am not sure that there is single moment you can make purchase and not have something better and cheaper coming out in next month or two. You will not spend your money on unnecessary purchase only if you don’t purchase anything. That is not destiny I wish to share.

And no, I am not sorry.