Patrick Braunstorfer
Patrick Braunstorfer

Reputation: 383

DispatcherTimer does not fire

I just got started in Windows Store App development and I just wanted a very simple application: Pretty much a progres bar that fills up from left to right, but even this task is apparently not achievalbe for me.

I have the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace TimeLoader
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer refreshTimer;

        public MainPage()
        {
            this.InitializeComponent();
        }

        void refreshTimer_Tick(object sender, object e)
        {
            TimePassedBar.Value += 5;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            TimePassedBar.Value = 50;
            new DispatcherTimer();
            this.refreshTimer = new DispatcherTimer();
            this.refreshTimer.Interval = new TimeSpan(0, 0, 0, 100);
            this.refreshTimer.Tick += refreshTimer_Tick;
            this.refreshTimer.Start();
        }
    }
}

<Page
    x:Class="TimeLoader.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TimeLoader"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Loaded="Page_Loaded">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ProgressBar Grid.Row="0" Grid.Column="0" Height="150" Value="75" VerticalAlignment="Center" Name="TimePassedBar"/>
    </Grid>
</Page>

Now the same Setup works pretty fine when I do it in WPF but the Tick Event never fires when I start this code built as a Windows store app. Is there something Special I have to pay Attention to when building Windows Store Apps? I have looked high and low and sadly have found nothing on this matter.

Upvotes: 3

Views: 1350

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70652

Your code was working fine. You just didn't wait long enough to notice. :)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    TimePassedBar.Value = 50;
    this.refreshTimer = new DispatcherTimer();
    this.refreshTimer.Interval = TimeSpan.FromMilliseconds(100);
    this.refreshTimer.Tick += refreshTimer_Tick;
    this.refreshTimer.Start();
}

You are setting the TimeSpan as 100 seconds. You need to use the five-parameter overload to get milliseconds. But IMHO it's easier and more readable to just use the FromMilliseconds() method (as above).

Also, you don't need to create a DispatcherTimer object twice, especially when you're going to ignore the first one completely. :)

Upvotes: 4

Related Questions