Quote:
|
Originally Posted by passel
Also, normally you would want to only use one timer in a program anyway.
|
Quote:
|
Originally Posted by PlausiblyDamp
Rather than have three timers each of which start and stop the other timers could you not just have a single timer
|
I agree with passel and PlausiblyDamp, three timers is two timers too many.
Also do you know there is such a thing as a
Timer class built in .Net?
So it's really not even necessary to use a Timer control if you don't want to...
I'm not a C# programmer but I quickly found this code
(inside "TrafficLight.zip (62.17K)" on port #3 of
this page):
Code:
private Timer lightTimer;
private TrafficLight trafficLight;
public TrafficLightController()
{
lightTimer = new Timer();
lightTimer.Interval = 1000;
lightTimer.Elapsed += lightTimer_Elapsed;
trafficLight = new TrafficLight();
}
In the "TrafficLight.cs" module for the above cited zip you may also
notice code like this:
Code:
namespace TrafficLightSim
{
public class TrafficLight
{
public ITrafficLight State
{ get; set; }
public Image Display
{
get
{
return State.Display;
}
}
public TrafficLight()
{
State = new TrafficLightOff();
}
public void Change()
{
State.Change(this);
}
}
}
In programming there is a concept know as a
state machine,
(or sometimes referred to as a
finite state machine).
Even though passel suggestion is a valid one:
Quote:
|
You would use a variable to track the state of things..
|
..the other option, instead of using a byte variable like "yState",
is to explore code for developing a state machine class
that creates States for each of the colors,
as shown in the C# code of
this off forum post.
If you really what to explore State Machines in C# I would also recommend
Part II of this CodeProject series.
Quote:
|
Originally Posted by passel
What type of control are your Lights, by the way.
|
I was curious about this also.
It's easy enough to just
draw the traffic lights using FillEllipse.
To see this C# traffic light drawing code in a more complete example you can download
"CSharpGraphics[Modified].zip" from the fourth post down on this
off forum page.