New MSDN forums launched!

Today Microsoft has released the next version of the MSDN forums:

Today my team has released Community Platform 1.0 with our all new Microsoft Forums 3.0 Web Application.

I’m the dev lead on the new forums app and we’ve had a ton of fun building this since most of us use forums in our daily lives and wanted to make this the best experience we could. We support branding for MSDN, Technet, Expression and a generic Microsoft brand for forums that don’t fall under the other areas. This code base was completely rewritten from scratch with a new Web 2.0 User Experience. Here are the links:

http://forums.msdn.microsoft.com
http://forums.technet.microsoft.com
http://forums.community.microsoft.com

This is a replacement to our current forums system found at http://forums.microsoft.com/msdn we’ll be migrating the old forums over within the next couple of months. [...]

Is this a tear that is running down my face. Yes, indeed, it is one! Finally, finally! Thanks for the effort. Kudos to the team. Migrate the data over very fast and kick the old system into the bin! :)

Published on Feb 16th, 2008 — Tags: , ,
Comments (3)    digg it!    kick it   

Cosmos: the C# OS that you can play with

Cosmos is an operating system project that aims to be completely implemented in CIL compliant languages, that is currently 100% C#.
[...]
Who doesn’t want to build their own OS? Cosmos provides all the legos and lets you forget the hard parts of writing low level kernel pieces, and unlike other OS’s does not force you into C++. Use C# (or any .NET language), plug the legos that you need, and build your shell!

Yay! What a great world :D The first public OS that is 100% written in C#. Isn’t it awesome; after the Singularity OS that has never seen the outside world (speaking of it has only been in Microsoft Research) this is finally something that we all can play with and give feedback.

A step-by-step guide on how to make it work on your PC

  1. Go to http://gocosmos.org/Vault/index.aspx and download the “Milestone 1″ release. It is a small zip file.
  2. Extract the zip file.
  3. Download and install Microsoft Virtual PC from here (Virtual PC is free).
  4. Start Virtual PC:
  5. Create a new virtual machine:







    Make sure to give it some RAM. The virtual hard disk doesn’t need to be that big. 20 MB or even less will do it :)
  6. Start the virtual machine by clicking on the “Start” button in the window with the title “Virtual PC Console”. The following window should appear:
  7. Click the “CD” menu and then the “Capture ISO Image” menu entry. In the following dialog navigate to the folder where you have extracted the Cosmos zip file and select the “Cosmos.iso”:
  8. Next click the “Action” menu and there the “Reset” menu entry. This should reset the virtual machine and after reset you should boot right into the Cosmos OS and end with a screen like this:

That’s it! You have set up Cosmos OS to run in Virtual PC. Happy playing with the operating system (hint: try the “help” command). :D Btw. you might encounter some issues with Cosmos but keep in mind that this is just a proof of concept and their first release!

If you want to play with the source code you can download it from the official project website at Codeplex. That’s also the place where you can give the team feedback.

Published on Feb 2nd, 2008 — Tags: , , , ,
Comments (3)    digg it!    kick it   

Futures of C#

Charlie Calvert is writing (in his blog) about some of the features that are planned for the next version of C# (probably named C# 4.0). In the first post of the series he is talking about dynamic features coming to the C# language:

static void Main(string[] args)
{
    dynamic
    {
        object myDynamicObject = GetDynamicObject();
        myDynamicObject.SomeMethod();         // call a method   
        myDynamicObject.someString = “value”; // Set a field
        myDynamicObject[0] = 25;              // Access an indexer
    }
}

The code snippet shows that in C# 4.0 you can use (if this feature gets implemented in this way) so called late-binding for all the code that is wrapped in a “dynamic” block. Late-binding means that the compiler is not checking during compile time if the methods are really part of the given type (in the example System.Object) but that these checks are done by the runtime during exection: this concept is already used in the DLR (Dynamic Language Runtime) and (somehow) when .NET reflection is used.

This new feature (it will be available also for other languages that build on the CLR) is so awesome. It really means that for highly dynamic frameworks or even other dynamic code you don’t need to write all this reflection code anymore.

The only downside that I can see is that when people use the feature without knowing what is going on behind the scenes they might end up with slow code and complain about .NET being slow. But probably with only allowing it in a dynamic block that isn’t much of a problem because you need to read up what this dynamic keyword and block exactly does. The team could also implement the compiler in a way that not all calls inside the dynamic blocks are going to be late bound but that the compiler checks if some calls could be bound during compile time and all the others are then converted to late bound calls.

The C# team is really having cool ideas and I’m curious about what Charlie is having as next feature in his pocket. :-)

Published on Feb 1st, 2008 — Tags: , ,
Comments (1)    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   

Programming fonts

Jeff Atwood (from Coding Horror) has revisited programming fonts and came to the conclusion that Consolas is the best font for programming.

I have to 100% agree with him. Since I switched to Consolas I wouldn’t switch back anymore. I have tried Courier New a few times afterwards and it was terrible. I even installed Consolas on the Windows XP Virtual PC installations to get rid of Courier New in the editors.

All the other fonts he is showing are terrible. He said he has to revisit the programming fonts because of Inconsolata. In my eyes that font really hurts in the eyes. It’s like Courier New with ClearType applied. Terrible!

What do you think?

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

Starting with game development

Ever wanted to start with game development? What technology could you use? There are several around. In the last months we have heard very much about XNA and XNA 2.0. But there are also other libraries around that are very interesting: two of them are DirectX or OpenGL.

For university a friend of mine and I had to write a small game named FishSalad. It’s a simple game and we had to write it in OpenGL. DirectX was theoretically allowed but everybody looked weird at you, if you considered using it (university life). I don’t regret having done the class and having written the game in OpenGL. The API has an easy to use model (it’s ideal to start with) and makes it easy to switch to DirectX afterwards: the calls are very similar. It’s always the same hardware in the end…

There are some very good OpenGL tutorial sites around. I like the collection of tutorials at NeHe’s site. It has all the important lessons from how to start with OpenGL (and C/C++) until how to write shaders, cool looking fogs and particle systems. Another interesting read is the OpenGL documentation: the so called Red Book.

DirectX on the other hand is also interesting if you want to program a game for Windows. There’s plenty of information on MSDN and there are a lot tutorial websites around. If you have time (and the required hardware) you should have a look at DirectX 10. The geometry shaders look very powerful and interesting.

If you want to start with XNA you should have a look at the link section at the XNA Development website. XNA Development itself has also some interesting and easy to understand tutorials. What makes XNA very interesting is the way how everybody can write small games in a few hours. Very cool!

As for techniques I really wonder when people start to implement relief mapping. It looks awesome and would be the next step after bump mapping. Anybody willing and having some free time?

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

Walking a multi-dimensional array in the snail-shell way

During an interview I was asked to walk a multi-dimensional array like in the “snail-shell” way (look at the image on the right side). Today I had some time and thought I could try to implement it.

What I did is partioning it in a call that does the first four arrows in the picture. The next recursive call does the next four and so on… I have also added some escape conditions to make sure stuff is not listed twice.

That’s what I got so far:

class Program
{
    static void Main(string[] args)
    {
        // The multi-dimensional array that is walked.
        int[][] table = {
                          new[] {  12345 },
                          new[] {  6789, 10 },
                          new[] { 11, 12, 13, 14, 15 },
                          new[] { 16, 17, 18, 19, 20 },
                          new[] { 21, 22, 23, 24, 25 }
                        };

        // Print that table.
        Print(table);
    }

    private static void Print(int[][] table)
    {
        // Print out the sub table. Starting the
        // recursion.
        PrintSubTable(table, 0);
    }

    private static void PrintSubTable(int[][] table, int iteration)
    {
        // Check if we are over the half. Leave then.
        if (iteration >= table.Length / 2.0f)
            return;

        // Read the first line of the iteration.
        int[] subtable = table[iteration];
        for (int i = iteration; i < subtable.Length - iteration; i++)
        {
            // Print all elements out.
            Console.WriteLine(subtable[i]);
        }

        // Read on the right side down.
        for (int i = iteration +1; i < table.Length - iteration -1; i++)
        {
            // Print the elements.
            Console.WriteLine(table[i][subtable.Length - iteration -1]);
        }

        // Check if we have to leave as we would read the last read line backwards.
        if (table[iteration] == table[table.Length -iteration -1])
            return;

        // Read the last line of the iteration backwards.
        subtable = table[table.Length - iteration -1];
        for (int i = subtable.Length - 1 - iteration; i >= iteration; i–)
        {
            Console.WriteLine(subtable[i]);
        }

        // Read the left side up.
        for (int i = table.Length - iteration - 2; i >= iteration +1; i–)
        {
            Console.WriteLine(table[i][iteration]);
        }

        // Proceed with the sub table.
        PrintSubTable(table, ++iteration);
    }
}

Published on Sep 6th, 2007 — Tags: , ,
Comments (8)    digg it!    kick it   

Singularity: A research OS written in C#

Microsoft researchers Jim Larus and Galen Hunt are leading a team where they’ve built a research operating system using managed code (at least most of it using managed code). The project is known as Singularity.

They are trying to achieve the following: Singularity is a research project focused on the construction of dependable systems through innovation in the areas of systems, languages, and tools. We are building a research operating system prototype (called Singularity), extending programming languages, and developing new techniques and tools for specifying and verifying program behavior.

On their website they have interesting papers (the one having the general overview, the rethinking of the software stack and deconstructing process isolation are very interesting) about how they were able to move most of the code into the managed world and what benefits they gained by doing it. For example they are using garbage collectors in all of the components of the operating system. Now you might think that that makes the OS a lot slower. But that is not true because it is no longer necessary to do context switches. The type safety and managed environment make sure that no application can access information that’s not intended for that application. Without context switches you can gain back speed and that allows the OS to run very fast.

Channel 9 is having some videos about Singularity:
- Singularity: A research OS written in C#
- Singularity Revisited
- Singularity III: Revenge of the SIP
- Singularity IV: Return of the UI

The videos are really great and show how the system works internally. If you have some free time, you should watch them! Very informative and a very new approach - a lot of “out of the box” thinking.

Published on Sep 2nd, 2007 — Tags: , ,
Comments (0)    digg it!    kick it