views:

114

answers:

2

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?

+4  A: 

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.
}
Jace Rhea
A: 

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.

Mick N