tags:

views:

107

answers:

2

hi guys,

I've developed a graph control in GDI+ and used a timer in it.. now i'm converting it to WPF. but when i went on and search for a timer there is no timers in WPF... how can i resolve this problem?any alternative to use?

regards, rangana.

+1  A: 

You can use System.Threading.Timer without any problems I suppose.

Here is an example of a timer which executes every 1 sec:

using System.Threading;

...

TimerCallback timerCallBack = OnTimerCallback;
Timer timer = new Timer(timerCallBack, null, 0, 1000);

...

private void OnTimerCallback(object state)
{
    ...
}

If you want to update any UI related elements from the timer, you will have to use Dispatcher.BeginInvoke because the timer runs in it's own thread and the UI is owned by the main thread which starts the timer. Here is an example:

private void OnTimerCallback(object state)
{
    Dispatcher.BeginInvoke(
     System.Windows.Threading.DispatcherPriority.Normal,
      (ThreadStart) (() => Background = Brushes.Black));
}
Yogesh
+1  A: 

The timer you are looking for is DispatcherTimer http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

But, if you plan to use a timer to drive animations, there are better ways to do that in WPF (just Google WPF animation).

Nir