Is there a timer control for Windows Phone 7?

前端 未结 2 1957
天命终不由人
天命终不由人 2020-12-09 16:02

I\'m trying to write a win phone 7 app for the first time - is there a timer control similar to the one for winforms? Or is there a way to get that type of functionality?

相关标签:
2条回答
  • 2020-12-09 16:55

    DispatchTimer is a good option as is Timer.

    It's worth being familiar with the differences and assessing which is more suitable for you.

    Convenience (DispatchTimer for UI updates) or Accuracy (Timer for predictability) is the crux of the decision.

    Timer Class (System.Threading)

    DispatcherTimer Class (System.Windows.Threading)

    The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.

    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 DispatcherTimer queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

    If a System.Threading.Timer is used, it is worth noting that the Timer runs on a different thread then the user interface (UI) thread. In order to access objects on the UI thread, it is necessary to post the operation onto the DispatcherTimer of the UI thread using Dispatcher.BeginInvoke. This is unnecessary when using a DispatcherTimer.

    0 讨论(0)
  • 2020-12-09 17:08

    You can use System.Windows.Threading.DispatcherTimer.

    System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
    dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
    dt.Tick += new EventHandler(dt_Tick);
    dt.Start();
    
    void dt_Tick(object sender, EventArgs e)
    {
            // Do Stuff here.
    }
    
    0 讨论(0)
提交回复
热议问题