1
votes

I wish to execute a function at an interval of 1 or so minutes. How can I achieve this in Windows Phone 8.

I am not looking for background agents. The app will be running in the foreground. What are my options?

4

4 Answers

10
votes

you can use the DispatcherTimer Class

private DispatcherTimer dispatcherTimer;
    // Constructor
    public MainPage()
    {

        InitializeComponent();
        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

    }

  private void dispatcherTimer_Tick(object sender, EventArgs e)
  {
    //do whatever you want to do here
  }

Refer: (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx)

5
votes

Try this

public void Start_timer()
{
    DispatcherTimer timer = new DispatcherTimer();
    timer.Tick += timer_Tick;
    timer.Interval = new TimeSpan(00, 0, 10);
    bool enabled = timer.IsEnabled;
    timer.Start();
}

void timer_Tick(object sender, object e)
{
    //function to execute
}
3
votes

You can also use ThreadPoolTimer:

TimeSpan period = TimeSpan.FromSeconds(60);

ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
{
    // TODO: Work

    Deployment.Current.Dispatcher.BeginInvoke(() => 
        {
            // UI update               
        });

}, period);
1
votes

One option may be to use DispatcherTimer.

Simply register a callback on the Tick event.