2
I’m building a stopwatch using WPF. When I performed some tests, I realized that the timer on WPF is delayed in relation to my timer iPhone. The difference starts to appear from 8 seconds after the start of the timing.
Below I made available the method code Timer_tick.
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 of 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 of 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 several tutorials but still can’t get a stopwatch where there was so much time difference. I don’t know if it’s due to the performance of WPF be considered to be less than that of Winform.
I had already made this same stopwatch on Winform and the time difference was imperceptible.
There is another problem, the timer is not displaying in format MM:SS:MMM. I don’t know if the .substring that I’m applying to the label is wrong or if the interval is wrong.
What interval is set in timer ? 1ms ?
– Isac
Why not use a Timespan instead of creating one?
– CypherPotato
guy.. will always delay.. Voce wants to tick 1 in 1 ms and the processing time is greater than that 1 ms between the ticks.. so there will always be this difference..
– Thiago Loureiro