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);    // If the switch is not on, turn the lamp off    System.Threading.Monitor.Enter(switchInterrupt);    if (!switchInterrupt.Read())    {       lampOutput.Write(false);    }    System.Threading.Monitor.Exit(switchInterrupt); } 

This method is what we want to execute every time we want the flashlight to flash. The method checks to see whether the lamp is already lit, and if it is not, flashes the lamp for half a second.

What we now need to do is create a timer instance and pass the timer a delegate that refers to this method.

 System.Threading.Timer timer = new System.Threading.Timer(      new System.Threading.TimerCallback(flashTick),      null, // object to pass into the timer      5000, // time in milliseconds before first tick      5000); // interval between ticks in milliseconds 

The constructor for the timer has four parameters. The first is the delegate that refers to the method that is to be called each time the timer fires. The second is a reference to an object that the timer method might need. For example, if the timer was counting ticks, we could create an object that held the ticks, and then on each call the timer method could update the tick value. We do not need to use this feature, and so we can set that parameter to the null value to indicate that we are not using an object.

The third and fourth parameters are the interval between the timer being created and the first tick being produced, and the interval between successive ticks, respectively. Both these values are expressed in thousandths of a second. The preceding code will cause the flashlight to flash every five seconds.

There is a second version of the timer constructor that allows you to pass timespan values into a new timer instance. A timespan is a structure that contains values for days, hours, minutes, seconds, and milliseconds, so you can create timer instances that will generate events very infrequently, perhaps once every week.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net