Hi Folks
I thought I understood Timer but the more I work with it the less I seem to know! Can anyone tell me how
to start a timer and then stop it when some condition is true? More specifically can you look at my code
below and tell me why I can't stop the timer using Timer.Main.aTimer.Enabled = false. I just can't seem
to figure out how to access the Timer once it has started. Help!
using System;
using System.IO;
using System.Threading;
using System.Timers;
public class Timer
{
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimer);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the timer"
while(Console.Read()!='q');
}
public static void OnTimer(Object source, ElapsedEventArgs e)
{
if (..some condition..)
{
// Stop the Timer
Timer.Main.aTimer.Enabled = false;
}
else
{
// Timer keeps going
....
}
}
}
Cheers,
Kenny.
I thought I understood Timer but the more I work with it the less I seem to know! Can anyone tell me how
to start a timer and then stop it when some condition is true? More specifically can you look at my code
below and tell me why I can't stop the timer using Timer.Main.aTimer.Enabled = false. I just can't seem
to figure out how to access the Timer once it has started. Help!
using System;
using System.IO;
using System.Threading;
using System.Timers;
public class Timer
{
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimer);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press \'q\' to quit the timer"
while(Console.Read()!='q');
}
public static void OnTimer(Object source, ElapsedEventArgs e)
{
if (..some condition..)
{
// Stop the Timer
Timer.Main.aTimer.Enabled = false;
}
else
{
// Timer keeps going
....
}
}
}
Cheers,
Kenny.