Crude timer

I need to execute some statements roughly about every 30ms…is this a reasonable means:

  TimeSpan deltadb = DateTime.Now.Subtract(debounce_time);
            if (deltadb.Milliseconds > 30)
            {
                debounce_time = DateTime.Now;

                // ** DO MY THINGS HERE ***
}

I don’t want to burn up a lot of time just checking the time & fear this will be wasteful

You should better use :


public class Foo
{
    void TimerElapsed(object o)
    {
        Debug.Print("Elapsed !");
    }
 
    public void Initialize()
    {
        Timer theTimer = new Timer(new TimerCallback(TimerElapsed), null, 0, 30);
        Debug.Print("Immediate timer that call back every 30ms");
        Thread.Sleep(Timeout.Infinite);
    }
}

Thanks!
I wonder how much of an overhead saver this is (if any).

By the way, I assume

Thread.Sleep(Timeout.Infinite);

is not needed since the rest of my program is busy running its things

by the way, I had to add “static” to make it work

static void TimerElapsed(object o)

thread.sleep(timeout.infinite) is needed if you no longer need to process anything in that particular thread. So if you run that code in it’s own thread, you should; if you call it in amongst your other code, then you’re correct and you don’t need it.

As Brett said !..not more ! :slight_smile:

Are there any good tutorials (with explanations) on using the timers & interrupts?

This one on MSDN :

http://blogs.msdn.com/b/laurelle/archive/2011/10/06/creating-and-launching-timer-in-net-microframework.aspx

or this one on tiny wiki :

In fact, there is not a major difference between timers in the normal and micro framework…The real goal is to use “best practices” in merging timers and threads together to make your system work properly…