DispatcherTimer and Timespan(whatever,whatever,whatever,Bad Dog!)

DispatcherTimer seems to have a singularly odd behavior when it comes to specifying the interval.  I was working on the Mah Jongg demo and needed a sub 1 second timer.  No problem I though, I’ll just define a 100ms timespan, as in new Timespan(0,0,0,100).

Hmmm –  Timer never fires the event. fiddle fiddle fiddle – nothing wrong with the code.

After some fuming , thought to define a Timespan via tick count, as in new TimeSpan(1000).  That worked fine.  That seemed odd, as I had been thinking that DispatcherTimer just didn’t like sub 1 second resolutions.  Not the case,  you can effectively lock up your UI with new TimeSpan(1).  (don’t do this on your gutless Sony portable – you won’t be happy)

So I wrote a test program, which you can find here.  What that shows is that if you use TimeSpan(hours,mins,secs,msecs), the DispatcherTimer won’t fire.  TimeSpan(hours,mins,secs) seems to work, as does TimeSpan(ticks) –  but if you provide any value for msec, even zero, it seems to be equal to NEVER.

Forewarned is forearmed – if you found this with a google search on ‘what the hell is wrong with DispatchTimer’ or ‘why the hell won’t my timer fire’ or something similar, try the test program – then use Ticks.

Published Tuesday, August 14, 2007 10:06 PM by MarkMMullin
Filed Under: ,

Comments