ILoggable

A place to keep my thoughts on programming

 Subscribe

geekblog
[at]
claassen [dot] net

Powered by Blogger

Thursday, October 26, 2006

A matter of timing

You know how everything time related in .NET uses milliseconds? Kind of gives you the impression you can deal with things at a millisecond resolution. Generally not so. The internal clock seems to have a resolution just a bit above 10ms, so no matter what value you use for Thread.Sleep() or as your interval for any of the three built-in timers, you achieve about 65Hz. Now, I say "generally" because while my tests on timers have returned 65Hz regardless of which machine I tried them on, Thread.Sleep(1) seemed to give me about 500Hz (still half as fast as it should be) on one machine.

For gathering timing statistics, you could always wrap the Kernel32 counters and get amazing time resolution and with .NET 2.0 you get System.Diagnostics.Stopwatch which has that high resolution as well, if Stopwatch.IsHighResolution is true. It seems to be true on every machine i've tried. But still doesn't give me a timer with the right resolution.

Thread.Sleep(0) seems to just yield execution, so a tight loop on it gives us high resolution and should let other threads work uninterrupted. But it does drive load up regardless. I guess it's a phantom load, but i can't be sure. Using System.Diagnostics.Stopwatch and Thread.Sleep(0), I whipped up a quick-and-dirty High Resolution Timer class as follows:

public class HiResTimer
{
  static Stopwatch ticktock = new Stopwatch();
  static int counter;
  static public void Test()
  {
    HiResTimer hiTee = new HiResTimer(new TimerCallback(DoSomething), 10);
    Thread.Sleep(500);
    for (int i = 0; i < 1000; i++)
    {
      Thread.Sleep(100);
      Console.Clear();
      double hz = counter * 1000 / ticktock.ElapsedMilliseconds;
      Console.WriteLine("hz: {0:f}", hz);
    }
    hiTee.Stop();
  }

  static void DoSomething(object nullobject)
  {
    if (!ticktock.IsRunning)
    {
      ticktock.Start();
    }
    counter++;
  }

  Thread timerLoop;
  Stopwatch watch = new Stopwatch();
  bool keepTiming = true;
  TimerCallback cycleDelegate;
  int interval;

  public HiResTimer(TimerCallback timerCycle, int interval)
  {
    this.cycleDelegate = timerCycle;
    this.interval = interval;
    timerLoop = new Thread(new ThreadStart(DoLoop));
    timerLoop.Start();
  }

  public void Stop()
  {
    keepTiming = false;
    timerLoop.Join();
  }

  private void DoLoop()
  {
    while (keepTiming)
    {
      watch.Reset();
      watch.Start();
      cycleDelegate(null);
      while (watch.ElapsedMilliseconds < interval)
      {
        Thread.Sleep(0);
      }
    }
  }
}
Seems to work and the built-in static Test() method reliably runs at 100Hz, but as i mentioned, my CPU is constantly at 50%. In theory, as the cycleDelegate increases in complexity and other threads consume processing power, the idle loop of the timer should yield it's processing hunger, but I have not given this a proper test.

The real question is, is there another sleep/yield mechanism that gives me a resoltution somewhere between Thread.Sleep(0) and Thread.Sleep(1) (where 1 really ends up being more like 12 in most cases).

Monday, October 09, 2006

Staying in sync

As I mentioned a couple of days ago, I'm trying to keep my PDA/phone and web in sync. The calendar portion is reasonably well taken care of. That leaves contacts and tasks.

I got one of my domains switched to Zimbra (fodder for another post) and looked at their address book. It seems to be just a folder with .vcf files. Using IMAP, I should be able to manipulate those fairly easily. I'll have to look whether i can access the contacts format on the xv6700 directly, because then I could just sync zimbra and my phone over the net. Alternatively, I hook into activesync on my PC and sync contacts only when I dock the phone.

And then there's Tasks. Zimbra doesn't seem to have an option for that. I know apple now stores tasks in iCal, but I guess nobody else has picked up on that yet. I looked around on the net and decided to use Voo2Do for my task keeping. It's got a nice simple REST api, that I was able to wrap in short time. Last night I moved that API to to .NET CF dll and got a little demo app running on my phone that fetches the contacts from Voo2Do. Yay! While not the most straight forward experience, I am nonetheless impressed how simple programming Pocket PC is, if you are already doing .NET stuff.

That means, tasks are well on their way to being taken care of and Contacts shouldn't be impossible to do as well.

So what were the less than straigh forward pieces in developing for the phone? Most likely it comes down to not having found any good resources for doing this and everything I've done for far has been cobbled together from various web searches and forum posts. So here's what I came across:

Gonna have to dig around a bit and find some better sources of documentation for doing this stuff.

Wednesday, October 04, 2006

How timely.. Using the xv6700 for net access

Engadget has a how-to on using a EV-DO device for internet access for a laptop. They use the xv6700 in their tutorial, so i need to try that out

Tuesday, October 03, 2006

Coding on the xv6700

As it turns out, my phone was not developer locked as I had assumed from some of the sites I found. It was mostly a matter of getting VS.NET 2k5 to install its code on the phone. Not that that went without problems, and I gather I still need to figure out how rapiconfig enters into this.

I created a demo on VS.NET 2k5, deployed it to the phone and it failed without any useful error. However the program was over there, so I ran it manually. Problem with my version of .NET Compact Framework, i.e. i only had 1.1 and needed 2.0 for what Visual Studio built. I searched for a while how to target 1.1 from VS.NET 2k5 and found references claiming that I could choose at project creation. However I found no such option when I tried a new project. I think have a release candidate of 2k5 on my home machine, so maybe it's supposed to be there and just not in my version. I will check on that tomorrow with my work machine, which has the official release on it.

So i fired up VS.NET 2k3. This one wouldn't connect to my phone. Played around with the settings for a while, but no go. So I hand deployed the build and voila, first test running on the phone.

Good. But I don't want to target 1.1 or use VS.NET 2k3, if i can avoid it. Can I upgrade my phone? I downloaded the .NET CF 2.o redistributable. It didn't want to install. It complained about another version being on my machine.. Grrr.. I don't care what my machine has, I want it on my phone! But I uninstalled the CF 2.0 I had, then re-installed the new one and it promptly upgraded my phone.

After the phone reboot, the phone no longer came up on my machine when i plugged it in. But that was just an artifact of the install, a rebbot fixed that. In the meantime, I fired up the binary I had previously built for 2.0 and now it worked as well. We've got a platform to play with!

Monday, October 02, 2006

Verizon xv6700 & calendaring

I got an xv6700 last week, trying to rid myself of the extra bulk of my palmpilot. The xv6700 is not too large, but has a nice large screen and a usefully sized keyboard. Plus its a full Windows CE 5.0 device, so I'll start writing apps for it as soon as i figure out how to unlock it for development.

In the meantime, my primary concern has been calendaring. That's been a pet peeve of mine for a while. I want to be able to setup meetings with people outside the boundaries of my personal calendaring program, but all the solutions used to be isolated to their servers. With iCal becoming widely accepted that's finally gone away. I can invite people that have an iCal product (zimbra, google, etc.) from the exchange server at work and vice versa and everything is happy.

However syncing to mobile still sucked, so my palm pilot and my online calendars were constantly out of sync. Switching to the xv6700 I hope to change that.

Now if I ran Exchange for personal use, all problems would be solved, but I don't. I'm currently playing with zimbra for personal use and there is still a use case that fails:

However inviting someone from exchange that uses zimbra works. So Mobile Outlook uses a different calendar file format, it would seem.. Annoying. However that still means that the only time I'm out of sync is when I create a new event on the phone. I can live with that for now.

Here's what google sees from the different type of invites:

Invite from Zimbra

Content-Type: text/calendar; name=meeting.ics; method=REQUEST; charset=utf-8
Content-Transfer-Encoding: 7bit
Invite from Exchange
Content-class: urn:content-classes:calendarmessage
Content-Type: text/calendar;
 name="meeting.ics";
 method=REQUEST
Content-Transfer-Encoding: 8bit
Invite from xv6700
Content-Type: application/ms-tnef
Content-Transfer-Encoding: base64
X-MS-Has-Attach:
X-MS-TNEF-Correlator: 221hvc0sdpi5
Content-Disposition: attachment

It would appear that at the very least, Mobile outlook doesn't set up the invite as an .ics, but instead uses MAPI and sends it as a TNEF file. So Exchange will send things out in a portable format, but Mobile Outlook won't. Pity. Can't find any options to change that behavior either.