Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Corey Roth

Related Topics: .NET

.NET: Article

Making the .NET Micro Framework Work for You

The ultimate flashlight - building a device

CREATING THREADS
The flashlight program has two threads, but one of them is in the body of the Main method. Although this will work well, situations might arise in which more than one thread is required, and from a design perspective it might be considered cleaner to create a separate thread to execute the flashlight-finder feature. The first thing to do is program the flashlight-finder feature in a method.

private static void finderFlash()
{
while (true)
{
// flashlight-finder feature goes here
}
}

The Main method can now create a thread instance and start it. The constructor of the thread receives the method to be executed in the thread. Once the thread instance has been created, it can then be started by calling its Start method.

System.Threading.Thread finderThread = new Thread(finderFlash);
finderThread.Start();

Note: It is important that any threads you create behave themselves. By this we mean that if they do not yield the processor by making calls to Sleep, they could cause performance problems in other threads.

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.

Summary
In this excerpt, we covered a lot of ground. We built a complete device that reads and responds to input signals and produces control outputs. We also used threads to allow several tasks to be performed in parallel and considered how to manage the synchronization issue that arise when threads must interact.

This excerpt is from Chapter 4 of Embedded Programming with the Microsoft .NET Micro Framework (ISBN# 0735623651) by Donald Thompson and Rob S. Miles, published by Microsoft Press, June 2007 (www.microsoft.com/MSPress/books/10457.aspx). Reprinted with permission.

More Stories By Donald Thompson

Donald Thompson is responsible for overseeing the end-to-end design and day-to-day management of the developers, software, protocols, and technology strategy fueling the SPOT initiative. During the Internet boom, he built the centralized ad serving system used by all MSN Web properties, including Hotmail, MSNBC, and MSN.com. Prior to joining Microsoft, he developed an automated loan kiosk and decisioning system for Citibank, a cellular billing and management system for Bell South, and wrote AI and 3D graphics algorithms for commercial game companies. Before committing to a life of software, he was a professional child actor working in movies and television.

More Stories By Rob Miles

Rob Miles is a Microsoft Most Valuable Professional. He is a lecturer and Teaching Fellow in the Department of Computer Science at the University of Hull and is presently responsible for delivering the first year programming course and he also lectures on software development and virtual machine architectures. Visit his blog at www.robmiles.com

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.