Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

May 2007 - Posts

How To Create a Really Accurate Timer

Some of the work on my current project requires accurately timing events, at a frequency of a few dozen per second (that means that each event has to follow the previous one within a few milliseconds). The resolution of those calls is so small, that even System.Threading.Timer can not handle it and lags behind.

After some searching, I found that Win32 Multimedia Timers are a good solution to this problem, as they are (probably) the timers with the highest resolution available on the platform. At the moment, there is no official wrapper for the API in the Framework and Leslie Sanford's article offers a nice implementation.

XmlSerializer Problems

Just stumbled onto an exotic bug in the framework:

using System;
using System.Xml.Serialization;
using System.IO;

namespace Repro
{
    class Program
    {
        static void Main(string[] args)
        {
            new XmlSerializer(typeof(StaticClass.NestedClass));
        }
    }

    public static class StaticClass
    {
        [Serializable]
        public class NestedClass
        {
        }
    }
}

Even though there will never be a need to instantiate StaticClass, this will fail with an InvalidOperationException stating that "Repro.StaticClass cannot be serialized. Static types cannot be used as parameters or return types."

Weird.

[Update: There's another one! The reason I moved to XmlSerializer instead of SoapFormatter was because SoapFormatter doesn't support generics and now:

namespace Repro
{
    class Program
    {
        static void Main(string[] args)
        {
            new XmlSerializer(typeof(C));
        }
    }

    [Serializable]
    public class A
    {
    }

    [Serializable]
    public class B<T> : A, IEnumerable<T>
    {
        // ...
    }

    public class C : B<int>
    {
        public void Add(int value)
        {
            // ...
        }
    }
}

The above code throws another InvalidOperationException, stating that 'To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy.' Moving the Add method to B works and I could understand it if I actually wanted to serialize an object of type B, but come on!

I've tried re-implementing IEnumerable<T> in C, but that doesn't work either.

This is quickly becoming very frustrating.]

More Posts