Click here to Skip to main content
15,880,469 members
Articles / General Programming / Performance

Microsecond and Millisecond C# Timer

Rate me:
Please Sign up or sign in to vote.
4.95/5 (85 votes)
14 Apr 2013CPOL6 min read 429.6K   21.3K   208  
MicroTimer: A microsecond and millisecond timer in C# that is used in a similar way to the .NET System.Timers.Timer.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MicroTimerWinFormsDemo
{
    public partial class FormMain : Form
    {
        // Declare MicroTimer
        private readonly MicroLibrary.MicroTimer _microTimer;

        public FormMain()
        {
            InitializeComponent();

            // Instantiate new MicroTimer and add event handler
            _microTimer = new MicroLibrary.MicroTimer();
            _microTimer.MicroTimerElapsed +=
                new MicroLibrary.MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent);
        }

        private void OnTimedEvent(object sender,
                                  MicroLibrary.MicroTimerEventArgs timerEventArgs)
        {
            // Do something small that takes significantly less time than Interval.
            // BeginInvoke executes on the UI thread but this calling thread does not
            //  wait for completion before continuing (i.e. it executes asynchronously)
            if (InvokeRequired)
            {
                BeginInvoke((MethodInvoker)delegate
                {
                    TextBoxElapsedTime.Text = timerEventArgs.ElapsedMicroseconds.ToString("#,#");
                });
            }
        }

        private void ButtonStartClick(object sender, EventArgs e)
        {
            long interval;

            // Read interval from form
            if (!long.TryParse(TextBoxInterval.Text, out interval))
            {
                return;
            }

            // Set timer interval
            _microTimer.Interval = interval;

            // Ignore event if late by half the interval
            _microTimer.IgnoreEventIfLateBy = interval/2;

            // Start timer
            _microTimer.Start();
        }

        private void ButtonStopClick(object sender, EventArgs e)
        {
            // Stop the timer
            _microTimer.Stop();
        }

        private void FormMainFormClosing(object sender, FormClosingEventArgs e)
        {
            // Stop the timer, wait for up to 1 sec for current event to finish,
            //  if it does not finish within this time abort the timer thread
            if (!_microTimer.StopAndWait(1000))
            {
                _microTimer.Abort();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions