I am developing a digital clock for windows 8 app.
And strangely some random second is being skipped in every minute. I am not sure why it is happening. One of my guesses is it might be because sometimes to retrieve that custom second image its taking more than a second and hence the next second gets skipped.
This is my code in XAML
<!-- second -->
<Grid Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="-15,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Column="0"
Source="{Binding CurrentTime, Converter={StaticResource ThemeTimeConverterSecondDigit1}}" Opacity="0.99" Stretch="None" />
<Image Grid.Column="1"
Source="{Binding CurrentTime, Converter={StaticResource ThemeTimeConverterSecondDigit2}}" Opacity="0.99" Stretch="None" Margin="-30,0,0,0" />
</Grid>
The binding variable Current Time gives me the time and i use a converter to get the custom image for that time.
This is the code for my converter
public object Convert(object value, Type targetType, object parameter, string language)
{
if (!(value is DateTime))
{
return value;
}
System.DateTime currentDate = DateTime.Now;
var dt = (DateTime)value;
var timePeriod = dt.ToString("tt ").StartsWith("A") ? TimePeriod.AM : TimePeriod.PM;
if (!this.IsTwentyFourHour && dt.Hour > 12)
{
dt = dt.Subtract(TimeSpan.FromHours(12));
}
switch (this.Component)
{
case ThemeComponents.SecondDigit1:
{
var sec = _GetFirstDigit(dt.Second);
return this.CurrentTheme.SmallDigits.Where(d => d.Value == sec).First().Image;
}
case ThemeComponents.SecondDigit2:
{
var sec = _GetSecondDigit(dt.Second);
return this.CurrentTheme.SmallDigits.Where(d => d.Value == sec).First().Image;
}
}
private int _GetFirstDigit(int number)
{
if (number >= 10)
{
return System.Convert.ToInt32(number.ToString().Substring(0, 1));
}
return 0;
}
private int _GetSecondDigit(int number)
{
if (number >= 10)
{
return System.Convert.ToInt32(number.ToString().Substring(1, 1));
}
return System.Convert.ToInt32(number.ToString().Substring(0, 1));
}
Please give me your views on why this might be happening or if u have any better ways to implement it. Thanks
EDIT:
I replaced the images with text blocks and gave just plain numbers and its still happening. The seconds are still being skipped
EDIT:
This is the code for how the Current time is set
var timeoutputShown = false;
_ClockTimer = new DispatcherTimer();
_ClockTimer.Interval = TimeSpan.FromMilliseconds(1000);
_ClockTimer.Tick += (s, e) =>
{
App.ViewModel.CurrentTime = DateTime.Now;
if (!timeoutputShown)
{
timeoutputShown = true;
//ShowTimePanelStoryboard.Begin();
}
};
Answer:
I reduced the interval from 1000ms to 100ms and that did it. Thanks a lot everyone.
If you are ticking your timer at 1 second intervals, then you effectively have a margin of error of 1 second too, and therefore the dispatcher queue only needs to be held for a small amount of time in order to drop a second from being displayed. You might be better to run your timer at a higher frequency so you have less of a window for error, and do less work because you are going to be called so frequently.
For example if you run your timer to tick every 0.1secs or 100ms then you can update the seconds up to 10 times a second, and then only draw screen updates when the value to display actually changes.
So for example in a clock that only shows seconds we could do;
Public void timer_tick(…)
{
MySeconds = DateTime.Now.Seconds:
}
Where
Public int MySeconds
{
set
{
// Only if the second has actually changed
if (_mySeconds != value)
{
_mySeconds = value;
NotifyPropertyChanged(…);
}
}
}
The trick is to do as little as possible at each stage.