| By Donald Thompson, Rob Miles | Article Rating: |
|
| May 18, 2007 02:00 PM EDT | Reads: |
12,230 |
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.
Published May 18, 2007 Reads 12,230
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications



























