Monthly Archives: January 2008

Lacking 64 bit support?

There is still a lot of libraries and technologies out there that does not work running in a 64 bit process. Take the Jet engine and utilizing it from OleDb; if you have a application that runs in a 64 bit native context, you will be given an error message that says that there is no OleDb provider for the provider specified in the connectionstring.

Another example is the use of the ExploreOM assembly in BizTalk 2006 used for controlling the BizTalk server programmatically. Used from a 64 bit native context the application will throw an exception.

How can one get around this problem?  Well. It’s actually very simple, if you’re willing to give away having a native 64 bit application or be invariant to the CPU. By choosing x86 as the CPU when compiling your .net application, you’ll be able to still use 32 bit only facilities. The process will then run in a 32 bit context and therefor run smoothly without any issues. Remember that it is only the executable (entry point) that needs to be compiled with this option.

You find the option under Project Properties -> Build -> Platform target.

 

image

A realtime light-weight StateMachine

I’ve been working the last week with a game for me to contribute with in the European Silverlight Challenge and figured I had to make a simple StateMachine “runtime” to undertake the the tasks I needed for the game.

The StateMachine concept I came up with is a very leightweight thingy that uses some reflection magic to accomplish a simple way of working with a StateMachine. All you need is an enum with all your states and then a class that implements the abstract generic StateMachine class. The generic type you specify is the actual enum that contains the states. Through reflection the StateMachine then looks for public instance methods in your class that is named in a special manner containing the name of the state. The collected information will then be used in the execution of the StateMachine.

You can download the entire implementation of the StateMachine

A sample StateMachine implementation using the system :

public enum SimpleStateMachineStates
{
    Idle=1,
    DoSomething,
    Die
}

public classSimpleStateMachine : StateMachine<SimpleStateMachineStates>
{
    public voidOnIdleEnter()
    {
        Console.WriteLine(“Entering idle mode – press spacebar to continue”);
    }

    public voidOnIdleRun()
    {
        ConsoleKeyInfo keys = Console.ReadKey();
        if(keys.Key == ConsoleKey.Spacebar)
        {
            this.ChangeState(SimpleStateMachineStates.DoSomething);
        }
    }

    public voidOnIdleLeave()
    {
        Console.WriteLine(“Leaving idle mode”);
    }

    public voidOnDoSomethingEnter()
    {
        Console.WriteLine(“DoSomething – Enter phase”);
        Console.WriteLine(“—“);
        Console.WriteLine(“Press enter to kill the process”);
    }

    public voidOnDoSomethingRun()
    {
        ConsoleKeyInfo keys = Console.ReadKey();
        if(keys.Key == ConsoleKey.Enter)
        {
            this.ChangeState(SimpleStateMachineStates.Die);
        }
    }

    public voidOnDoSomethingLeave()
    {
        Console.WriteLine(“DoSomething – Leave”);
    }

    public voidOnDieEnter()
    {
        Console.WriteLine(“Killing”);
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }

    public overrideSimpleStateMachineStates DefaultState
    {
        get{ returnSimpleStateMachineStates.Idle; }
    }
}

A realtime light-weight StateMachine

I’ve been working the last week with a game for me to contribute with in the European Silverlight Challenge and figured I had to make a simple StateMachine “runtime” to undertake the the tasks I needed for the game.

The StateMachine concept I came up with is a very leightweight thingy that uses some reflection magic to accomplish a simple way of working with a StateMachine. All you need is an enum with all your states and then a class that implements the abstract generic StateMachine class. The generic type you specify is the actual enum that contains the states. Through reflection the StateMachine then looks for public instance methods in your class that is named in a special manner containing the name of the state. The collected information will then be used in the execution of the StateMachine.

You can download the entire implementation of the StateMachine

A sample StateMachine implementation using the system :

public enum SimpleStateMachineStates
{
    Idle=1,
    DoSomething,
    Die
}

public classSimpleStateMachine : StateMachine<SimpleStateMachineStates>
{
    public voidOnIdleEnter()
    {
        Console.WriteLine(“Entering idle mode – press spacebar to continue”);
    }

    public voidOnIdleRun()
    {
        ConsoleKeyInfo keys = Console.ReadKey();
        if(keys.Key == ConsoleKey.Spacebar)
        {
            this.ChangeState(SimpleStateMachineStates.DoSomething);
        }
    }

    public voidOnIdleLeave()
    {
        Console.WriteLine(“Leaving idle mode”);
    }

    public voidOnDoSomethingEnter()
    {
        Console.WriteLine(“DoSomething – Enter phase”);
        Console.WriteLine(“—“);
        Console.WriteLine(“Press enter to kill the process”);
    }

    public voidOnDoSomethingRun()
    {
        ConsoleKeyInfo keys = Console.ReadKey();
        if(keys.Key == ConsoleKey.Enter)
        {
            this.ChangeState(SimpleStateMachineStates.Die);
        }
    }

    public voidOnDoSomethingLeave()
    {
        Console.WriteLine(“DoSomething – Leave”);
    }

    public voidOnDieEnter()
    {
        Console.WriteLine(“Killing”);
        System.Diagnostics.Process.GetCurrentProcess().Kill();
    }

    public overrideSimpleStateMachineStates DefaultState
    {
        get{ returnSimpleStateMachineStates.Idle; }
    }
}

VS2008 QFE Debugging on a WCF application

I got all excited today and downloaded and instaled VS2008 QFE (.net source code) and enabled debugging according to Shawn Burke’s blog and fired up a WCF server application I’m working on. The server is using the ServerHost class for hosting it. Much to my surprise I kept getting an ConfigurationErrorsException with the message : “This element is not currently associated with any context”. The application hadn’t changed for a couple of days and I have no configuration file as I define everything programmatically. The only thing I had done was to install QFE and enable Debugging.

Turns out that the “Enable just my code” option that I turned off to enable QFE debugging caused this. I haven’t really bothered to figure out why yet, just thought I’d blog the incident straight away.

VS2008 QFE Debugging on a WCF application

I got all excited today and downloaded and instaled VS2008 QFE (.net source code) and enabled debugging according to Shawn Burke’s blog and fired up a WCF server application I’m working on. The server is using the ServerHost class for hosting it. Much to my surprise I kept getting an ConfigurationErrorsException with the message : “This element is not currently associated with any context”. The application hadn’t changed for a couple of days and I have no configuration file as I define everything programmatically. The only thing I had done was to install QFE and enable Debugging.

Turns out that the “Enable just my code” option that I turned off to enable QFE debugging caused this. I haven’t really bothered to figure out why yet, just thought I’d blog the incident straight away.

Missing Enum.GetValues() when doing Silverlight for instance ?

In some implementations of the .net framework, convience methods such as the static method GetValues() on the Enum class has been stripped away. This turned out to be true in the Silverlight subset of the framework. A method I’ve grown attached to over the years..   

There is a way of getting the values from an Enum through reflection; by using the GetFields() method on the type of the enum you wish to get the values from.

The GetFields() method returns an array of FieldInfo. An enum will have an array of all it’s values represented as fields. In addition it will return elements that are not values on the enum. Within FieldInfo you’ll find a set of Is*() methods, one of them are very interesting in this case; IsLiteral. All values are marked as true. The solution is then pure and simple (C# 3.0 syntax, requires LINQ) :

public static class EnumHelper
{
    public static T[] GetValues<T>()
    {
        Type enumType = typeof(T);

        if (!enumType.IsEnum)
        {
            throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
        }

        List<T> values = new List<T>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;

        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            values.Add((T)value);
        }

        return values.ToArray();
    }

    public static object[] GetValues(Type enumType)
    {
        if (!enumType.IsEnum)
        {
            throw new ArgumentException("Type '" + enumType.Name + "' is not an enum");
        }

        List<object> values = new List<object>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;

        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            values.Add(value);
        }

        return values.ToArray();
    }

}

Xbox Live GamerCard .net class (C#)

A friend of mine; Dan Strandberg put together a nice Vista Sidebar gadget for showing your Xbox Live GamerCard (Look here).

I couldn’t resist creating a C# class that can be used from any .net application to extract all the data. Based upon Dans (nice regexes.. 🙂 ) work I’ve put together a simple class that one can use for this purpose (download here). I’ve also put together a simple WinForms application that uses it (download here).

XboxLiveGamerCard.netclassC_12020_image_2

The usage is very simple :

GamerCard.Loaded += new GamerCardLoadedEventHandler(GamerCard_Loaded);
GamerCard card = GamerCard.Create("Adept DoLittle");

void GamerCard_Loaded(GamerCard gamerCard)
{


}

 

There is a house…

… in Sandefjord, Norway that’s looking for a new owner. 🙂

After living in the same house for 6 years, we’re now selling it and moving to the other side of town. Besides working heavily on my computer over the last years, the house has also gone from a early 1980s combined with early 1950s standard to 200x standard. I’ve done most of the work myself. Finally we finished it and then we’re moving, sounds familiar ?? 

Anywho..  If you know anyone who wants a nice house here in Norway, our house might be the thing they’re looking for.  🙂

Have a look :

http://webhotell.tinde.no/dnbnor/index.asp?pId=274824&offset=0