I’m on Facebook

Yes it’s incredible - even to me - but I’m on Facebook. I have joined because of the TechEd group that’s organizing themselves there…

You want to be my friend there? Add me :) But psssst, don’t tell anybody - it is a secret!

Published on Oct 23rd, 2007 — Tags:
Comments (0)    digg it!    kick it   

Cross-thread operations in .NET

Have you ever gotten the exception that you can’t invoke a control’s method from a background thread in .NET? You know the “Cross-thread operation not valid” exception. You’re not alone! There are reasons why this behaviour has been introduced. Like the background thread could lock your UI without any obvious reason and the program would just look hung and nobody would actually understand why.

There are several ways to walk-around this problem. One would be, for example, to use the BackgroundWorker class and communicate with the UI via the ProgressChanged event. Another way is to check if an “Invoke” method call is required and call the control’s (Form inherits from Control) Invoke method by specifying a method and creating a delegate for the method’s signature. That would look like this:

if (this.InvokeRequired)
    this.Invoke(new UpdateControlsDelegate(UpdateControls));
else
    this.UpdateControls();

The UpdateControls method (in this example) would update the controls on the form.

Always checking for whether an Invoke is required and then doing the appropriate thing or even creating own delegates for method signatures is a lot of work. But we can do better.

That’s when C#’s anonymous methods come into the game. They allow us to create a method and even pass in arguments to the method’s body (this second fact is very useful in our scenario). These methods are delegates and can be executed as delegates that can then communicate with the UI. I have written a short class that encapsulates the functionality. The class holds just two delegates: one returns a generic value and one returns nothing (void). The arguments for the methods called inside the anonymous method are just passed in and the C# compiler will take care for us. :-)

/// <summary>
/// This class automates cross thread operations.
/// </summary>
public static class CrossThreadOperation
{
    public delegate void Func();
    public delegate TResult Func<TResult>();

    /// <summary>
    /// Invokes a cross-thread operation that doesn’t return a result.
    /// </summary>
    /// <param name="control"></param>
    /// <param name="del"></param>
    public static void Invoke(Control control, Func del)
    {
        if (control == null)
            throw new ArgumentNullException(“control”);
        if (del == null)
            throw new ArgumentNullException(“del”);

        // Check if we need to use the controls invoke method to do cross-thread operations.
        if (control.InvokeRequired)
            control.Invoke(del);
        else
            del();
    }

    /// <summary>
    /// Invokes a cross-thread operation that returns a result.
    /// </summary>
    /// <typeparam name="TResult"></typeparam>
    /// <param name="control"></param>
    /// <param name="del"></param>
    /// <returns></returns>
    public static TResult Invoke<TResult>(Control control, Func<TResult> del)
    {
        if (control == null)
            throw new ArgumentNullException(“control”);
        if (del == null)
            throw new ArgumentNullException(“del”);

        // Check if we need to use the controls invoke method to do cross-thread operations.
        if (control.InvokeRequired)
            return (TResult)control.Invoke(del);
        return del();
    }
}

The usage is very straight forward. If we have a method without arguments and without return value, only the method name needs to be specified. Everything else is done by the C# compiler.

// "this" is the form (or control) that we operate in.
CrossThreadOperation.Invoke(this, UpdateControls);

If we have an argument for the internally called method (or methods) we need to use the delegate keyword:

int value = 10;
CrossThreadOperation.Invoke(this, delegate { UpdateControls(value); });

And if we expect some return value, we need to use the Invoke method that expect a generic argument.

int value = 10;
int result = CrossThreadOperation.Invoke<int>(this,
             delegate { return UpdateControls(value); });

Published on Oct 19th, 2007 — Tags: , ,
Comments (7)    digg it!    kick it   

Virtualization and Windows 7

This is a pretty neat video of Eric Traut from Microsoft talking about virtualization. He gave this speech for the Association for Computing Machinery at the University of Illinois. In the video he’s demoing also a super stripped down version of Windows 7. Apparently Windows 7 will scale to 256 core machines.

Look at the nice ASCII art in the boot screen :-)

Published on Oct 18th, 2007 — Tags: ,
Comments (0)    digg it!    kick it   

F# is now the real deal: It’s an official .NET language

F# is now an official language of the .NET framework and therefore becomes a first citizen in the .NET framework :-) - This is great news. Now we’ll get F# with each .NET distribution and even in the Visual Studio (I hope so). Yippee!

I knew that functional programming is cool, when I first coded something in Haskell a few years ago.

A great move and let’s hope they make the best out of it. It’s also cool to see that stuff from MSR (Microsoft Research) is published in an actual product. I know there are other technologies too that came from MSR and where published in some kind of product. But, you know, lately it seemed as if everything that was nice and developed by MSR went just somewhere… somewhere where nobody could ever find it again.

Anyway: great news, great move!

Published on Oct 17th, 2007 — Tags: ,
Comments (0)    digg it!    kick it   

The Code Project Visual Studio 2008 Beta Competition

The Code Project is having a competition. Yay! You can win $10,000 by writing an article on .NET 3.0 and the Visual Studio 2008…

Everybody in?

Published on Oct 16th, 2007 — Tags: ,
Comments (0)    digg it!    kick it   

A Visual Explanation for Joins in SQL

Jett Atwood at Coding Horror did it. He showed what actually happens when you join two tables with the different SQL join keywords (like inner join, outer join etc.). He used Venn diagrams to give a visual representation. They are broadly used in statistics and one of my favourite way to display intersections.

A great article and that might finally bring light into the dark of joins! ;-)

Published on Oct 12th, 2007 — Tags:
Comments (0)    digg it!    kick it   

An incredible cool Halo 3 review!

It’s awesome. Get it from here. I’ll not going to buy Halo 3 anymore ;-)

Published on Oct 11th, 2007 — Tags: ,
Comments (0)    digg it!    kick it   

I’m a switcher

Yeah! And I’m proud of it. I switched!

I switched from my master “media computer science” to “computer graphics”. Why? Some of the lectures in my old master sound nicer than they really were. Lucky me I did (mostly) only the lecture that are shared between the two masters which allows me to take them to the new master :-) Some are lost and some will be added to my list of “elective courses”.

It isn’t putting me much behind shedule and it is (hopefully) going to be more interesting, although it is going to be harder because there is a lot more math in computer graphics than in media computer science…

In some courses there are even only 4 people… I’m wondering why… and I’ll know soon - today is one of these courses!

Anyway: game engines - I’m coming!

Published on Oct 11th, 2007 — Tags:
Comments (0)    digg it!    kick it