-1

I've set up my timer as such and it works fine. However I need the interval to fire more frequently so that the clock counts down quicker (as it wouldn't be appropriate for the user to have wait for the timer to countdown in real time, as it is a simulation). I hope I've explained it appropriately, please ask if you have any questions. Any help would be very much appreciated!

    System.Windows.Forms.Timer timer;
    TimeSpan countdownClock = TimeSpan.Zero;

    private void Form1_Load(object sender, EventArgs e) 
    {
        timer = new System.Windows.Forms.Timer();                                               //Initialise timer
        timer.Interval = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;
        timer.Tick += OnTimeEvent;
        DisplayTime();
    }

    private void DisplayTime()
    {
        lblTime.Text = countdownClock.ToString(@"hh\:mm\:ss");                                  //Define timer
    }

    private void OnTimeEvent(object sender, EventArgs e)
    {
        countdownClock = countdownClock.Subtract(TimeSpan.FromMilliseconds(timer.Interval));    //Set up countdown

        if (countdownClock.TotalMilliseconds <= 0)
        {
            countdownClock = TimeSpan.Zero;
            timer.Stop();
            Button_Open.Enabled = true;
        }
        DisplayTime();
    }
L. Mitchell
  • 61
  • 1
  • 10
  • In other words you want to display time value more frequently? How about using another (faster) timer? In limited environment one would use fast timer initially and apply divider, e.g. timer is set to 100 ms, every 10 events (divider = 10) check for timeout. In your case divider may not even be required. Btw, countdown can be more resistant to timer frequency if instead of calculating it you simply store time and just calculate it as `DateTime.Now - storedDateTime`. Another option is to use `Stopwatch` (start it together with timer, use `Elapsed` to display value, stop with timer). – Sinatr May 12 '17 at 12:55
  • `timer.Interval = less then 1000` – EpicKip May 12 '17 at 13:06
  • @EpicKip I've attempted to just add in a just a `int` value and it doesn't have the desired effect – L. Mitchell May 12 '17 at 13:11
  • 1
    @That doesn't make any sense as you're casting the timespan to int so you are using an int value. Debug and check how many that is... – EpicKip May 12 '17 at 13:14
  • I realise that it doesn't really make sense, but it just won't work for some reason, thanks for your help though – L. Mitchell May 12 '17 at 13:27

1 Answers1

1

There are two things here that deal with "time":

  • The frequency at which the timer ticks
  • The rate at which the countdown value decreases

You can make the timer tick more frequently by reducing the interval:

// the timer will tick 3 times as often
timer.Interval = (int)(TimeSpan.FromSeconds(1).TotalMilliseconds / 3);

The frequency of the timer does not affect the speed at which the countdown reaches zero, because on each tick of the timer you subtract the interval of that timer from the countdown. If you'd subtract more from it, you could speed up the time required for the countdown to reach zero:

// countdown will reach 0 three times as fast as it would normally
countdownClock = countdownClock.Subtract(TimeSpan.FromMilliseconds(timer.Interval * 3));

Note that the System.Windows.Forms.Timer isn't very accurate and might not tick as often as you tell it to, especially if you use intervals of only a few milliseconds.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Thanks for your help! I've tried implementing the first option and it doesn't seem to have any effect? Also I've tried the second one and it won't let me use the`*` operator in that context – L. Mitchell May 12 '17 at 13:24
  • Ah sorry I had misplaced it, moved it to the right spot now. The first option only causes your `DisplayTime()` to be called more often. You'll probably notice once you implement the second. – C.Evenhuis May 12 '17 at 13:30
  • Oh that's great!! It works! However in the instance `* 3` it counts down in 3 sec intervals rather than every second. Is there a way to still make it count down every second? – L. Mitchell May 12 '17 at 13:33
  • Ah I've found that I can sort it by using a combination of both solutions! – L. Mitchell May 12 '17 at 13:35
  • Glad to hear, yeah both cover one aspect of the "time experience" :) – C.Evenhuis May 12 '17 at 13:54