Reputation: 3490
I have a DispatcherTimer
in my UWP app for updating database from a web services.
public DispatcherTimer SessionTimer { get; set; }
Before updating the DB in the tick event of this timer, I collapse main grid and show updating message and after update completed I do reverse.
private void SessionTimer_Tick(object sender, object e)
{
rpWait.Visibility = Visibility.Visible;
LayoutRoot.Visibility = Visibility.Collapsed;
Update();
rpWait.Visibility = Visibility.Collapsed;
LayoutRoot.Visibility = Visibility.Visible;
}
My XAML codes:
<Grid x:Name="LayoutRoot" Style="{StaticResource backGrid}">
<RelativePanel Style="{StaticResource rpTop}">
...
</RelativePanel>
<Frame x:Name="frameBody" Loaded="frameBody_Loaded" Margin="0,100,0,0"/>
</Grid>
<RelativePanel x:Name="rpWait" Visibility="Collapsed">
<StackPanel RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True">
<TextBlock x:Name="lbMessage" FontSize="30" HorizontalAlignment="Center">Updating</TextBlock>
<TextBlock x:Name="lbWaiting" FontSize="30" Margin="0 50 0 0">Please Wait</TextBlock>
</StackPanel>
</RelativePanel>
But the timer does not show anything (DB updated properly).
Help me please?
Upvotes: 0
Views: 713
Reputation: 3998
I believe your problem is that you dont run Update() Asynchronous and await for the update. Also if if you do UI updates better to do them in the UI thread.
DispatcherTimer ds = new DispatcherTimer();
// In Constuctor of the page
ds.Interval = new TimeSpan(0, 1, 0);
ds.Tick += ds_Tick;
ds.Start();
void ds_Tick(object sender, object e)
{
ShowHide(true);
await Update();
ShowHide(false);
}
private async void ShowHide(bool state)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
if(state)
{
rpWait.Visibility = Visibility.Visible;
LayoutRoot.Visibility = Visibility.Collapsed;
}
else
{
rpWait.Visibility = Visibility.Collapsed;
LayoutRoot.Visibility = Visibility.Visible;
}
});
}
private Task Update()
{
//run your update
//if your update doesnt have await use
//return Task.Run(() => {
// //doto
// });
//If it does just write your code
}
Upvotes: 1