Monday, November 26, 2007

The Missing Stopwatch and the Missing Milliseconds


While I've benefited tremendously from having my T-Mobile Wing, one of the things it's missing is a convenient stopwatch feature. My old cell phone provided a nice stopwatch, but there's nothing like this on the Wing. Even my Garmin GPS has a stopwatch feature, so I figure my cell phone should get one too.

This seems like a simple enough project and an excellent opportunity to jump into writing a C# project on the .NET Compact Framework. Well, the first thing I found out was that the DateTime object's Milliseconds property is always zero on a T-Mobile Wing. Some quick web searching revealed that this isn't a Wing peculiarity, but a lot of Windows CE devices don't provide millisecond resolution for calls to GetLocalTime() (which is ultimately beneath .NET's DateTime class).

A nice summary of alternatives can be found here. The simplest solution is to use System.Environment.TickCount to simply get the number of milliseconds that have passed since the operating system started. The only caveat is that the TickCount is suspended when the system goes into suspend mode. So if your device suspends, your stopwatch will stop ticking. However, for my needs this is sufficient for the time being. I'll look at workarounds in the future if I should need one.

Mobile Stopwatch can be found in the Mobile Tools download from this blog.

No comments: