Make a timer go once

How do I make a timer call only once


Answer this question

Make a timer go once

  • El locolito

    Your timer needs to be a member variable, not just in the scope of a method.

    This means that

    Timer clock = new Timer();

    goes inside the class, but outside of any methods.

    Sorry, I just assumed it was, but I see now it wasn't.



  • Dan Hadfield

    I tried that:

    The name 'clock' does not exist in the current context


  • Jon Russell

    System.Timers.Timer t = new System.Timers.Timer();

    t.AutoReset = false;

    t.Elapsed += new System.Timers.ElapsedEventHandler(sc_TikTak);

    t.Interval = 1000;

    t.Start();

     

     

     

    I think it is the best way to do it :)


  • Jeff B.

    private void start(object sender, EventArgs e) {

    clock.Stop();



  • denszon

    Yup, that's the best way. And if you add the timer via the windows forms designer, you can set the AutoReset property right in the properties page for the timer.

  • Make a timer go once