Timer issue
Can anyone help me find out why my timer is decreasing by 2 instead of 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PowerOps
{
public partial class SystemShutDown : Form
{
int timeLeft;
bool timerRunning = false;
public SystemShutDown()
{
InitializeComponent();
shutDown.Interval = 1000;
shutDown.Tick += shutDown_Tick;
}
private void shutDown_Tick(object sender, EventArgs e)
{
timer(sender, e);
}
public void timer(object sender, EventArgs e)
{
if (timeLeft > 0)
{
timeLeft--;
timerTextLabel.Text = $"Time Left: {timeLeft / 60:D2} minutes {timeLeft % 60:D2} seconds";
}
else
{
MessageBox.Show("Time's up!", "Countdown Complete");
timerRunning = false;
shutDown.Stop();
}
}
public void startButton_Click(object sender, EventArgs e)
{
if (!timerRunning)
{
int countDown = int.Parse(timerBox.Text) * 60;
timeLeft = countDown;
timerTextLabel.Text = $"Time Left: {timeLeft / 60:D2}:{timeLeft % 60:D2}";
shutDown.Interval = 1000;
shutDown.Start();
timerRunning = true;
}
}
} }
} }
0 Replies