0

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?

n.y
  • 3,343
  • 3
  • 35
  • 54

1 Answers1

1

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
}
Stamos
  • 3,938
  • 1
  • 22
  • 48
  • Maybe the insert happents too fast? Try adding Task .Sleep(5000) below insert and check agian. – Stamos Mar 01 '16 at 06:29
  • No It's take 2 minutes. – n.y Mar 01 '16 at 08:04
  • I ll ask again, do you do the insert async? if you put a breakpoint at start of method and do 'step over' will it pass Insert instantly or will wait for it to finish? and then swap visibilitys again – Stamos Mar 01 '16 at 08:08