简体   繁体   中英

Windows Phone Timer interval not consistent

I'm looking to create a windows phone application using C#. I want to have a timer that shows one image for 100 milliseconds then switches to another image and then waits another 900 milliseconds before it flashes the image again. I have the code below written, however, it doesn't seem to flash consistently. Any ideas?

public partial class MainPage : PhoneApplicationPage
{
    DispatcherTimer timer = new DispatcherTimer();
    List<string> files = new List<string>() { "Images/off-light.png", "Images/on-light.png" };
    List<BitmapImage> images = new List<BitmapImage>();
    int current = 0;

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        foreach (string file in files)
        {
            BitmapImage image = new BitmapImage(new Uri(file, UriKind.Relative));
            images.Add(image);
        }


            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(900);
            timer.Tick += new EventHandler(timer_Tick);

    }

    void timer_Tick(object sender, EventArgs e)
    {
        myImage.Source = images[current];
        current++;
        if (current >= files.Count)
        {
            current = 0;
            timer.Interval = TimeSpan.FromMilliseconds(100);
            timer.Stop();
            timer.Start();
        }
        else
        {
            timer.Interval = TimeSpan.FromMilliseconds(900);
            timer.Stop();
            timer.Start();
        }

    }

    private void btnStop_Click(object sender, RoutedEventArgs e)
    {
        timer.Stop();
        myImage.Source = images[0];
    }

    private void btnStart_Click(object sender, RoutedEventArgs e)
    {
        timer.Start();
    }
}

The DispatchTimer documentation says:

Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

I don't know if that's what's causing your problem, as I haven't ever worked with DispatchTimer .

You do have other options for timers. For example, you could use System.Timers.Timer or System.Threading.Timer (which I recommend over System.Timers.Timer ). Do note, though, that if you use one of these timers the callback will be executing on a pool thread and you'll need to synchronize access to the UI thread. Again from the DispatchTimer documentation:

If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.

You might consider increasing the priority of your timer?

i'm not much of a winforms/wpf/silverlight guy, but i'd say you need to invalidate your window.

call whatever method is needed to refresh the rendering, as I'd think changing the source image doesn't trigger a refresh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM