none
Timer in WP7

    Question

  • 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.

     

    Thanks

    Tuesday, May 25, 2010 7:57 PM

Answers

All replies

  • Hi,

    See the Timer class in System.Threading.

    Thanks,

    Mike

    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;
    tmr.Start();

     

    And the timer function as below.

     void OnTimerTick(object sender, EventArgs e)
    {

    }

    Enjoy timer!!!! 

    Wednesday, May 26, 2010 12:48 PM