Below is a sample of some of the code I have written to try to invoke a timer function that calls a specified method every time it elapses.
The method is Lights called by new ElapsedEventHandler. During execution, when this section of the code is reached it does not call the method Lights. I am looking for any suggestions for why this is so, or alternatively a version of code that utilises the timer function.
private Vehicle aVehicle;
private System.Timers.Timer aTimer;
private int simlength = 0;
System.Timers.Timer myTimer = new System.Timers.Timer();
public void LightTimer(Queue vehicleQueue)
{
Queue VQueue=vehicleQueue;
try
{
aTimer= new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(Lights);
aTimer.Interval = 1000;
myTimer.Enabled = true;
}
catch (Exception e)
{
Console.WriteLine("Due to a technical fault it was not possible to initialise traffic light timing sequence. "+e);
}
}
public void Lights(Object source, ElapsedEventArgs e)
{
simlength++;
if (simlength < 21)
this.LightSequence();
}
The method is Lights called by new ElapsedEventHandler. During execution, when this section of the code is reached it does not call the method Lights. I am looking for any suggestions for why this is so, or alternatively a version of code that utilises the timer function.
private Vehicle aVehicle;
private System.Timers.Timer aTimer;
private int simlength = 0;
System.Timers.Timer myTimer = new System.Timers.Timer();
public void LightTimer(Queue vehicleQueue)
{
Queue VQueue=vehicleQueue;
try
{
aTimer= new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(Lights);
aTimer.Interval = 1000;
myTimer.Enabled = true;
}
catch (Exception e)
{
Console.WriteLine("Due to a technical fault it was not possible to initialise traffic light timing sequence. "+e);
}
}
public void Lights(Object source, ElapsedEventArgs e)
{
simlength++;
if (simlength < 21)
this.LightSequence();
}