Timer takes 10 ms more then interval

Posted by Praveen on Stack Overflow See other posts from Stack Overflow or by Praveen
Published on 2010-06-07T12:18:15Z Indexed on 2010/06/07 12:22 UTC
Read the original article Hit count: 273

Filed under:

Hi All,

I am using a timer with interval 50 miliseconds. But in the timer's tick event when I print the time it's always 62 or 65 ms. I don't understand why it's taking 10 ms more.

Please can some one have look into this.

Here is the code I am using.

static int _counter;
        System.Timers.Timer _timer = new System.Timers.Timer(1000);
        public Form1()
        {
            InitializeComponent();           
            _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
            _timer.Start();            
        }

        void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine(DateTime.Now.ToString("{hh:mm:ss.fff}"));          
            _counter++;
            if (_counter == 20)
                _timer.Stop();            
        }

And this the output:

{01:59:08.381}

{01:59:09.393}

{01:59:10.407}

{01:59:11.421}

{01:59:12.435}

{01:59:13.449}

{01:59:14.463}

{01:59:15.477}

{01:59:16.491}

{01:59:17.505}

{01:59:18.519}

{01:59:19.533}

{01:59:20.547}

{01:59:21.561}

{01:59:22.575}

{01:59:23.589}

{01:59:24.603}

{01:59:25.615}

{01:59:26.629}

{01:59:27.643}

© Stack Overflow or respective owner

Related posts about c#