News Flash: System.Threading is ... threaded
AKA: "Why you shouldn't trust a driver guy to teach you how to write code in C#"
A while ago I wrote this cute little text game in C# for my Smartphone. I used it for a long time with no difficulty. But, when I recently moved it to a new device, it would occasionally hang. Of course, I could never get it to happen under the debugger, but this week I went searching through all the code in the hang path. It didn't make any sense. The code would have needed to be multithreaded to hang. So I called Brian Cross and asked if System.Threading.Timer ran in a different thread than the rest of the process.
Now Brian is a good friend, so he just said, "Let me check ... yes." What he should have said was, "Duh! It even says 'Threading' in the name!" Much to my chagrin and embarrassment, I was writing multithreaded code without realizing it.
I'm sharing this embarrassment with all of you for two reasons. First, don't make the mistake I did. If you're looking for a managed equivalent to WM_TIMER messages, you want System.Windows.Forms.Timer, not System.Threading.Timer.
But the bigger deal is that I wrote a blog entry a while back called Power to the Developers part 1 in which I provided a sample that showed how to stop doing CPU intensive stuff after a period of inactivity. And, yes, I used the multithreaded timer. So not only was I doing it wrong, I was teaching all of you how to do it wrong too. <sighs>
Anyway, I've updated the managed side of the Animate Sample to use the Forms timer instead of the Threading timer. If you've ever downloaded that sample, please get the new version. Sorry for the confusion.
Mike Calligaro