Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer - how do I stop it?!

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top