C# - TimeSpan doesn't work with seconds for more than one day
I think the title of this question isn't good, but I couldn't find a
better one.
What I'm trying to do is to give users the ability to set a count-down
timer. ( Look at picture No.1 ):
The application takes the user input and checks which time unit did the
user select, then converts it into seconds and assign the value to an int
variable.
private int seconds = -1;
private void enable_button_Click(object sender, EventArgs e)
{
int amount = Convert.ToInt32(time_numericUpDown.Value);
string unit = tUnits_comboBox.Text;
// Check what time is chosen then convert it to seconds
if (unit == "Seconds")
seconds = amount;
else if (unit == "Minutes")
seconds = amount * 60;
else if (unit == "Hours")
seconds = amount * 3600;
else if (unit == "Days")
seconds = amount * 86400;
// Clock it!
timer.Enabled = true;
}
Then, the timer should show the time in a human-readable format, I use
this code for that:
private void timer_Tick(object sender, EventArgs e)
{
// Verify if the time didn't pass
if (seconds == 0)
{
// If the time is over, do the specified action
timer.Enabled = false;
operation(); // << This is the function that does the Action!
}
else
{
// Continue counting
seconds -= 1;
TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
timeSpan.Hours,
timeSpan.Minutes,
timeSpan.Seconds);
status_label.Text = "Restarting in " + answer;
}
}
This works perfectly when the value of the "seconds" variable represents
one day or less, but when it's more than 24 hours it just shows 24 hours
in the status. What am I doing wrong?
( The problem ):
No comments:
Post a Comment