I am building a timer using WPF . When I ran some tests, I noticed that the timestamp in WPF lags behind the timer of my iPhone . The difference starts appearing after 8 seconds after the start of the timing.
Below I've made available the Timer_tick method code.
private void Timer_Tick(object sender,EventArgs e)
{
//lblTime.Content = DateTime.Now.ToLongTimeString();
//lblTime.Content = DateTime.Now.ToString("HH:mm:ss:fff");
//milliseconds++;
//if (milliseconds >= 1000) {
// sec++;
// milliseconds = 0;
//}
//if (sec == 60) {
// min++;
// sec = 0;
//}
//lblTime.Content = string.Format("{0:00}:{1:00}:{2:D3}",min,sec,milliseconds);
milliseconds += 15.560;
if (milliseconds >= 1000) {
milliseconds = 0;
sec++;
if (sec >= 60) {
sec = 0;
min++;
}
}
lblTime.Content = min.ToString("00") + ":" + sec.ToString("00") + ":" +
milliseconds.ToString("000").Substring(0,3);
//lblTime.Content = String.Format("{0:00}:{1:00}:{2:000}", timer.Interval.Minutes, timer.Interval.Seconds, timer.Interval.Milliseconds);
}
Below the code for the 'Start' button:
private void Start(object sender,EventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
This is the code for the entire class:
public partial class MainWindow :Window
{
DispatcherTimer timer = new DispatcherTimer();
int min = 0, sec = 0;
double milliseconds = 0;
public MainWindow()
{
InitializeComponent();
}
private void Timer_Tick(object sender,EventArgs e)
{
//lblTime.Content = DateTime.Now.ToLongTimeString();
//lblTime.Content = DateTime.Now.ToString("HH:mm:ss:fff");
//milliseconds++;
//if (milliseconds >= 1000) {
// sec++;
// milliseconds = 0;
//}
//if (sec == 60) {
// min++;
// sec = 0;
//}
//lblTime.Content = string.Format("{0:00}:{1:00}:{2:D3}",min,sec,milliseconds);
milliseconds += 15.560;
if (milliseconds >= 1000) {
milliseconds = 0;
sec++;
if (sec >= 60) {
sec = 0;
min++;
}
}
lblTime.Content = min.ToString("00") + ":" + sec.ToString("00") + ":" +
milliseconds.ToString("000").Substring(0,3);
//lblTime.Content = String.Format("{0:00}:{1:00}:{2:000}", timer.Interval.Minutes, timer.Interval.Seconds, timer.Interval.Milliseconds);
}
private void Start(object sender,EventArgs e)
{
timer.Interval = TimeSpan.FromMilliseconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
}
I've tried a lot of tutorials but I still can not get a timer where there is not so much time difference. I do not know if it is due to the WPF performance being considered smaller than the WinForm .
I had already done this same timer in WinForm and the time difference was imperceptible.
There is another problem, the timer is not displaying in the MM: SS: MMM format. I do not know if the .substring that I am applying in the label update is wrong or if interval is wrong.