Timer in WP7


  • Hi, im trying to make a Media Player and I need a Timer, but I cant use the reference System.Timer, If someone can help me with that I'll appreciate it.



    Tuesday, May 25, 2010 7:57 PM


All replies

  • Hi,

    See the Timer class in System.Threading.



    Tuesday, May 25, 2010 8:22 PM
  • Thanks

    Tuesday, May 25, 2010 8:35 PM
  •  in WP7 we can create timer in two ways.

    Method 1. Timer in System.Threading

        You can create timer like below 

      Timer tm = new Timer(TimerCallBack);
       tm.Change(Time to start , Timeout period);

     Please not there is no dedicated start function for this timer, as soon as we set the timer it will start.

    But the problem for this timer (Not the problem, actually the behavior of this timer)  in the call back function it cannot access the control variable. For example if u have a button in the page, U cannot access button1.text or any other properties of it. For doing that in the call back function we need to call as below

     public void TimerCallBack(object state)
                this.Dispatcher.BeginInvoke(delegate() { OnTimer(); });


    In this new timer event function we can access the page controls.


    Method 2. DispatcherTimer  (System.Windows.Threading)

     This is more straight forward and we can pause n resume the timer 

    It is used as below

    DispatcherTimer tmr = new DispatcherTimer();
    tmr.Interval = TimeSpan.FromSeconds(1);
    tmr.Tick += OnTimerTick;


    And the timer function as below.

     void OnTimerTick(object sender, EventArgs e)


    Enjoy timer!!!! 

    Wednesday, May 26, 2010 12:48 PM