5 Replies - 374 Views - Last Post: 05 April 2012 - 11:53 AM Rate Topic: -----

#1 MacNeil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 12

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

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();
            }
            
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Timer

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,128
  • Joined: 27-January 10

Re: Timer

Posted 05 April 2012 - 10:23 AM

You want to fire off this method inside the Timer's Tick event. :)

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

Was This Post Helpful? 0
  • +
  • -

#3 MacNeil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 12

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();

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1055
  • View blog
  • Posts: 2,235
  • Joined: 30-January 11

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 -

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

Was This Post Helpful? 1
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1212
  • View blog
  • Posts: 4,128
  • Joined: 27-January 10

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.

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

Was This Post Helpful? 1
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,410
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1