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

  • SEARCH
  • Home
  • 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 8257699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:17:12+00:00 2026-06-08T02:17:12+00:00

Apologies in advance for rather long post and lot of code. My application has

  • 0

Apologies in advance for rather long post and lot of code.

My application has a timed autosave feature. Users asked that I provide a visual indicator of how much time is left. I did some research on count down timers and eventually wrote the class below:

public class CountDownTimer
    {

        private Timer timer;
        private int remaining;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down ticked delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="remaining">    The remaining. </param>
        /// <param name="maximum">      The maximum. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownTickedDelegate(int remaining, int maximum);

        /// <summary>   Event queue for all listeners interested in Ticked event. </summary>
        public event CountDownTickedDelegate Ticked;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down percent delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="percent">  The percent. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownPercentDelegate(int percent);

        /// <summary>   Event queue for all listeners interested in Percent events. </summary>
        public event CountDownPercentDelegate Percent;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Count down done delegate. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public delegate void CountDownDoneDelegate();

        /// <summary>   Event queue for all listeners interested in Done events. </summary>
        public event CountDownDoneDelegate Done;

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets the maximum value to count down from </summary>
        ///
        /// <value> The maximum value. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public int Maximum
        {
            get;
            set;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Gets or sets a value indicating whether the timer is Paused. </summary>
        ///
        /// <value> true if paused, false if not. </value>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public bool Paused
        {
            get;
            set;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Starts this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Start()
        {
            timer = new Timer {
                Interval = 1000
            };
            timer.Tick += onTimerTick;
            timer.Enabled = true;
            remaining = Maximum;
            Paused = false;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Stops this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Stop()
        {
            if (timer == null)
            {
                return;
            }

            Paused = true;
            timer.Enabled = false;
            timer = null;
            if (Percent != null)
            {
                Percent(100);
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Resets and restarts this CountDownTimer. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Reset()
        {
            Stop();
            Start();
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Handles the timer tick event. </summary>
        ///
        /// <remarks>   Jon, 18/06/2012. </remarks>
        ///
        /// <param name="sender">   Source of the event. </param>
        /// <param name="e">        Event information to send to registered event handlers. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        private void onTimerTick(object sender, EventArgs e)
        {
            if (remaining > 0)
            {
                if (Ticked != null)
                {
                    Ticked(remaining, Maximum);
                }

                if (Percent != null)
                {
                    int percent = remaining * 100 / Maximum;
                    Percent(percent);
                }

                if (!Paused)
                {
                    remaining--;
                }
                else
                {
                    remaining = Maximum;
                }
            }
            else
            {
                Stop();

                if (Done != null)
                {
                    Done();
                }
            }
        }
    }

I am using a Timer and each time it ‘fires’ I decrement a counter. Each decrement kicks off an event so that my Form can present it visually. When the counter reaches zero another event kicks off the autosave.

There are a few other bits included to allow the autosave to be restarted if the user manually saves or if they open a new project.

It seemed to work for me. However a user is reporting that the longer the timer runs the shorter the interval between autosaves. I set the timer to tick every second and my investigations show that it runs at twice the speed. So if the counter is set to 60 (seconds) then it runs down every 30. I cannot replicate the behavior seen by the user but his log certainly shows things running much too fast.

This is in the same thread as the main app – is that likely to be an issue. All my tests so far have not turned anything up other than the tick seems to fire twice in a row every third time or so.

Many thanks in advance for any insight.

  • 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-06-08T02:17:14+00:00Added an answer on June 8, 2026 at 2:17 am

    One problem I see, is that if CountDownTimer.Start() is called two (or even multiple) times, without the appropriate CountDownTimer.Stop() calls, you end up with two or more activated instances of your Timer object, both invoking your onTimerTick() event handler.

    This could cause your described effect, as all running Timer instances decrease the remaining iterations separately.

    Is that possible with your calling code?

    EDIT:

    As a workaround I would suggest, you call Stop() from within Start(). Or even better, you do not recreate the Timer object for every new count down action. Create the Timer object in the constructor and only manipulate its properties.

    It is also not a bad idea to remove the onTimerTick() event handler from the timer instance when you dispose the Timer object. Otherwise the GC can not collect the timer instance as it still holds a reference to its CountDownTimer instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies in advance for the relatively long post - I've tried to provide as
First, a thank you in advance. Second, this is my first post so apologies
Apologies in advance for a rather non-specific question: I am looking for some guidance
Apologies in advance for a policy, rather than a programming question, but given the
OK, apologies in advance for posting a huge chunk of code! My problem is
Greetings, Apologies in advance that I have not researched this toughly enough to answer
This is my first post. Apologies in advance if my question is dumb. I'm
Apologies in advance for the amount of code at the end, because there is
Apologies in advance for the long-winded question. I'm really a database programmer, but have
Apologies in advance for a long question: I do want to give all the

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.