private void HandleLoadScreen()
{
startScreen.Hide();
activeScreen = loadScreen;
loadScreen.Show();
//begin timer
stopWatch.Start();
if (stopWatch.Elapsed.Seconds>2L)//if time is greater than 2 seconds initiate next code
{
loadScreen.Hide();
activeScreen = actionScreen;
actionScreen.LoadLevel(Levels[currentLevel]);
loadScreen.Hide();
actionScreen.Show();
}
}
Timer
Page 1 of 15 Replies - 374 Views - Last Post: 05 April 2012 - 11:53 AM
#1
Timer
Posted 05 April 2012 - 10:15 AM
Hi, I'm trying to implement a basic timer that begins once a method is entered. Once the timer counts to 2 seconds I would like it to initiate a second statement as shown below
Replies To: Timer
#2
Re: Timer
Posted 05 April 2012 - 10:23 AM
Scratch that, please specify if you're using the Timer class, the StopWatch class or what. Be specific, and if possible link to the MSDN page for us.
This post has been edited by Sergio Tapia: 05 April 2012 - 10:25 AM
#3
Re: Timer
Posted 05 April 2012 - 10:34 AM
i'm using the stopwatch but am willing to change it if there is a better option
Stopwatch stopWatch = new Stopwatch();
#4
Re: Timer
Posted 05 April 2012 - 10:48 AM
Sounds to me like you want to be using the Timer.
To call that method every time the Timer 'ticks' you need to make use of the Timer.Tick event handler -
To call that method every time the Timer 'ticks' you need to make use of the Timer.Tick event handler -
Timer timer = new Timer()l
timer.Interval = 2000;
timer.Tick += TimerTickEventHandler;
// Start the timer somewhere
...
...
private static void TimerTickEventHandler(Object myObject, EventArgs myEventArgs)
{
// do some stuff here
}
This post has been edited by Ryano121: 05 April 2012 - 10:50 AM
#5
Re: Timer
Posted 05 April 2012 - 11:01 AM
Don't forget that "Tick" will fire off every X interval you set. And seems to me that by your code you want to fire something off once, when a time is met.
Just declare a bool variable and set it to false once you fire the method once. Then subsequent "Tick"s won't fire your method again.
Or better yet, disable the Timer from within the Tick event.
Ask for code if you need help.
Just declare a bool variable and set it to false once you fire the method once. Then subsequent "Tick"s won't fire your method again.
Or better yet, disable the Timer from within the Tick event.
private static void TimerTickEventHandler(Object myObject, EventArgs myEventArgs)
{
//Fire off your method.
//Disable this timer from running.
timerInstaceName.Enabled = false;
}
Ask for code if you need help.
This post has been edited by Sergio Tapia: 05 April 2012 - 11:03 AM
#6
Re: Timer
Posted 05 April 2012 - 11:53 AM
Your second option there is vastly better than the first. There's no reason to keep the timer firing if it doesn't need to.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|