A question about timer to define

Hi, I have read the define of “System Timers” in “NETMF Tutouials”, can I only define a timer like as follow:

public class Program
{
    static void RunMe(object o)
    {
        Debug.Print("From timer!");
    }
    public static void Main()
    {
        Timer MyTimer =
           new Timer(new TimerCallback(RunMe), null, 5000, 1000);
        Debug.Print(
               "The timer will fire in 5 seconds and then fire priodically every 1 second");
        Thread.Sleep(Timeout.Infinite);
    }
}

How can I stop the" MyTimer"?

How can I define a timer like this:

private Timer Timer1;

this.Timer1 = new Timer { Enabled = false, Interval = 1000 };

this.Timer1.Start();
this.Timer1.Stop();

The timer stops by disposing it. It starts as soon as the clr runs the new command.

The enable can be tested in the callback method, if enable is false, then call the timer in the callback method, in this way, timer is reseted until enable is true.
If enable is true, call the desired function.

Or use Gadgeteer.Timer.

workhard10

I use something like this:



 private static bool useDebug = true;

 // Read Data timer
 Timer aTimer;

 // Stop timer
 //  Infinite dueTime
 const int Infinite = -1;

 // Initial Timer tick interval
 int TimerPeriod = 400;

 // Create the timer
 aTimer = new Timer(new TimerCallback(TimerTick), null, 5000, TimerPeriod);

 // Set timer dueTime to 'Infinite' to stop the timer
 aTimer.Change(Infinite, 0);  // STOP timer

 // Set dueTime to 0 for starting immediately
 aTimer.Change(0, TimerPeriod);  //START timer immediately

 // Set mydueTime to something other than 0 for starting when you want
 int mydueTime = 4000;
 aTimer.Change(mydueTime, TimerPeriod);  //START timer after delay


 void TimerTick(object o)
 {
     // To do what you want

     if (useDebug) { Debug.Print("Data Timer Tick"); }
  }


I beleive this is correct… Off the top of my head…

@ willgeorge -

thank you very much!!!