Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4257176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:27:18+00:00 2026-05-21T05:27:18+00:00

I wanted a timer with the following properties: No matter how many times start

  • 0

I wanted a timer with the following properties:

  1. No matter how many times start is called, only one call back thread is ever running

  2. The time spent in the call back function was ignored with regards to the interval. E.g if the interval is 100ms and the call back takes 4000ms to execute, the callback is called at 100ms, 4100ms etc.

I couldn’t see anything available so wrote the following code. Is there a better way to do this?

/**
 * Will ensure that only one thread is ever in the callback
 */
public class SingleThreadedTimer : Timer
{
    protected static readonly object InstanceLock = new object();
    
    //used to check whether timer has been disposed while in call back
    protected bool running = false;

    virtual new public void Start()
    {
        lock (InstanceLock)
        {
            this.AutoReset = false;
            this.Elapsed -= new ElapsedEventHandler(SingleThreadedTimer_Elapsed);
            this.Elapsed += new ElapsedEventHandler(SingleThreadedTimer_Elapsed);
            this.running = true;
            base.Start();
        }
        
    }

    virtual public void SingleThreadedTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        lock (InstanceLock)
        {
            DoSomethingCool();

            //check if stopped while we were waiting for the lock,
            //we don't want to restart if this is the case..
            if (running)
            {
                this.Start();
            }
        }
    }

    virtual new public void Stop()
    {
        lock (InstanceLock)
        {
            running = false;
            base.Stop();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:27:19+00:00Added an answer on May 21, 2026 at 5:27 am

    Here’s a quick example I just knocked up;

    using System.Threading;
    //...
    public class TimerExample
    {
        private System.Threading.Timer m_objTimer;
        private bool m_blnStarted;
        private readonly int m_intTickMs = 1000;
        private object m_objLockObject = new object();
    
        public TimerExample()
        {
            //Create your timer object, but don't start anything yet
            m_objTimer = new System.Threading.Timer(callback, m_objTimer, Timeout.Infinite, Timeout.Infinite);
        }
    
        public void Start()
        {
            if (!m_blnStarted)
            {
                lock (m_objLockObject)
                {
                    if (!m_blnStarted) //double check after lock to be thread safe
                    {
                        m_blnStarted = true;
    
                        //Make it start in 'm_intTickMs' milliseconds, 
                        //but don't auto callback when it's done (Timeout.Infinite)
                        m_objTimer.Change(m_intTickMs, Timeout.Infinite);
                    }
                }
            }
        }
    
        public void Stop()
        {
            lock (m_objLockObject)
            {
                m_blnStarted = false;
            }
        }
    
        private void callback(object state)
        {
            System.Diagnostics.Debug.WriteLine("callback invoked");
    
            //TODO: your code here
            Thread.Sleep(4000);
    
            //When your code has finished running, wait 'm_intTickMs' milliseconds
            //and call the callback method again, 
            //but don't auto callback (Timeout.Infinite)
            m_objTimer.Change(m_intTickMs, Timeout.Infinite);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use Swing Timer and I wanted to start from a
I have an imagemap and wanted to keep track of how many times it
I wanted to write a timer in java.which will do the following: when program
I have the following piece of code, just wanted to check who will call
This is a trivial question but I wanted to double check. How many times
Many, many times, I have wanted to create a template that takes a list
I wanted to divert www.mydomain.com/alpha/ to www.mydomain.com/. So I did following in .htaccess file:
I wanted to know the behavior of std::vector::reserve() in following situations: Suppose reserve(N) is
I got started by following this awesome tutorial , but wanted to do the
i wanted to make a simple data entry application. So i did the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.