Implementing a Flashlight-Finder Feature Using a Timer
The thread solution works, but .NET has a much better way of creating events on a timed basis, by using the Timer
class. A timer object will serve as an event source in that it can make calls to a handler at regular intervals. A timer behaves much like an interrupt pin, except that the events are triggered by the passage of time, rather than by an external event. The method that is attached to the event is slightly different; it accepts a parameter that refers to an object.
private static void flashTick(object o) { if (switchInterrupt.Read()) // Go round again if the switch is on return; // Switch on the lamp lampOutput.Write(true); // Wait for a 10th of a second System.Threading.Thread.Sleep(100); ...
Get Embedded Programming with the Microsoft® .NET Micro Framework now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.