locked
Fastest timer interval RRS feed

  • Question

  • This should be a fairly simple question. Basically, I'm wondering if there are any timers available within a c# application that can actually fire their event handler faster than the 10 or 15 millesecond interval I'm experiencing.

    I started with a windows forms timer. I noticed that when setting the interval to 1ms, it was still only firing its event handler about once every 15 milleseconds. On other computers I understand it might be 10 ms, but that's depending on your processor, or some other factor.

    Next I tried a System.Timers.Timer, and I also noticed it was not executing any faster than once every 15 ms. Finally, I tried a System.Threading.Timer, and it doesn't run any faster either.

    I'm developing a Windows Forms application that requests information frequently from a serial port. However, it seems I can only choose an actual timer interval of 15 ms, or 30 ms, or 45 ms, etc.

    So, the question is simply whether there are any timers that aren't limited by this interval, or whether I'm just doing something wrong.

    Consider this console application. It simply starts a thread timer, set to perform its action once every 1ms. In reality, it's only writing about 60-80 lines per second.

    Thanks for any advice.


    Code Snippet

    using System;

    namespace ConsoleApplication1
    {
        class Program
        {
            static System.Threading.Timer timer;
            private static int _counter;

            static void Main(string[] args)
            {
                _counter = 0;

                System.Threading.TimerCallback cb = new System.Threading.TimerCallback(ProcessTimerEvent);
                timer = new System.Threading.Timer(cb, null, 1, 1);

                Console.Read();
            }

            private static void ProcessTimerEvent(object obj)
            {
                _counter++;
                Console.WriteLine(_counter.ToString());
            }
        }
    }



    Friday, February 29, 2008 9:40 PM

Answers

  • These are really only the built in timers you have.  You may just restricted by the amount of time the CPU is giving to the process, the itme it takes to do stuff, and what else is going on.  Out of the box, you probably wont get much better.

     

    Friday, February 29, 2008 9:44 PM

All replies

  • These are really only the built in timers you have.  You may just restricted by the amount of time the CPU is giving to the process, the itme it takes to do stuff, and what else is going on.  Out of the box, you probably wont get much better.

     

    Friday, February 29, 2008 9:44 PM
  • For accurate timing, don't use a multitasking operating system.  If you need accurate timing interface to a microcontroller.

    Friday, February 29, 2008 10:10 PM