Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a timer control for Windows Phone 7?

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?

like image 619
Nils Avatar asked Jul 16 '10 14:07

Nils


2 Answers

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.
}
like image 191
Jace Rhea Avatar answered Nov 10 '22 17:11

Jace Rhea


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.

like image 36
Mick N Avatar answered Nov 10 '22 19:11

Mick N