How to reset the timer count down?

jdoe doe 20 Reputation points
2024-01-10T17:16:28.5533333+00:00

When it finish counting down from 1 minute to 00:00:00 then restart the timer over again counting back 1 minute. and I want it to start automatic over again inside the tick event and i want to know also how to call it from other places in the application and restart it from there again to count backward 1 minutes or any other times. The timer interval is set to 100 and I start the timer somewhere in my form1 code in a button click event. The timer is already in the form1 designer.

DateTime target = DateTime.Now + TimeSpan.FromMinutes(1);
private void timer1_Tick(object sender, EventArgs e)
{
    var countdown = target - DateTime.Now;

    // Display time with milliseconds
    lblTimer.Text = $"{countdown.Hours:D2}:{countdown.Minutes:D2}:{countdown.Seconds:D2}";//:{(int)countdown.TotalMilliseconds:D3}";
}
Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. jdoe doe 20 Reputation points
    2024-01-10T19:34:13.5066667+00:00

    this is working.

    DateTime target = DateTime.Now + TimeSpan.FromMinutes(1);
    private void timer1_Tick(object sender, EventArgs e)
    {
        var countdown = target - DateTime.Now;
    
        if (countdown <= TimeSpan.Zero)
        {
            // Reset the target to 1 minute from now
            target = DateTime.Now + TimeSpan.FromMinutes(1);
        }
    
        // Display time without milliseconds
        lblTimer.Text = $"{countdown.Hours:D2}:{countdown.Minutes:D2}:{countdown.Seconds:D2}";
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.