Monday, December 30, 2013

Timer Skips a Second

Hey guys,


I'm trying to get my timer to measure seconds but it skips two seconds if I press stop and three times if I press stop again etc.




timer = new DispatcherTimer();
ts = new TimeSpan(1000000);
timer.Interval = ts;

private async void btnBegin_Click(object sender, RoutedEventArgs e)
{
if (hasBegun == false)
{
if (cmbxSubjects.SelectedValue != null && cmbxActivities.SelectedValue != null)
{
timer.Start();
timer.Tick += timeData_Tick;
btnBegin.Content = "Stop";
hasBegun = true;
btnLogSession.IsEnabled = false;
}

else if (cmbxSubjects.SelectedValue == null || cmbxActivities.SelectedValue == null)
{
await pleaseEnterAllFields.ShowAsync();
}
}

else
{
btnLogSession.IsEnabled = true;
btnBegin.Content = "Start";
hasBegun = false;
timer.Stop();
}
}

public void timeData_Tick(object sender, object e)
{
if (seconds == 60)
{
seconds = 0;
lblSeconds.Text = seconds.ToString();

minutes++;
lblMinutes.Text = minutes.ToString();
}

else if (seconds < 60)
{
seconds++;
lblSeconds.Text = seconds.ToString();
}

if (minutes == 60)
{
minutes = 0;
lblMinutes.Text = minutes.ToString();

hours++;
lblHours.Text = hours.ToString();
}

totalSeconds++;
}

How can I fix it?


No comments:

Post a Comment