Refresh data in windows store app

﹥>﹥吖頭↗ 提交于 2020-01-14 03:55:11

问题


Im creating a windows store app that has some webservice calls.Currently i use a button event to make the webservice calls, but i want the app to do this automatically every 5 minutes. The button event is now placed in MainPage.xaml.cs What is the best way to perform this are there any timer event in windows store apps?

  public async void GetWebServiceDataButton(object sender, RoutedEventArgs e)
    {
     ........
     .........
     Retrieve data from web service
    }

回答1:


It's a bit much code to paste here so I'll add the appropriate links first and I'll provide for those using HTML/JS as well.

For those using HTML/JS See: Timers using setTimeout in a Windows 8 app

Using cs/xaml although you can still use js for this depending how you want to accomplish this: How to submit a work item using a timer

For repeating timers see: How to create a periodic work item

Now in your case do you need this to happen even when the application is not running? If so, then you'll need a background task which runs at most once every 15 minutes or so Create and register a background task

So a basic scenario is the following (from the msdn pages) but if you want a way to cancel it, that's provided there as well on the site. Note this will stop running when your application is suspended or terminated which is when your application has been closed OR is no longer the active application for around five seconds.


var period = TimeSpan.FromMinutes(5);

var timer = ThreadPoolTimer.CreatePeriodicTimer((source) =>
    {
        //do your query/work here
        Dispatcher.RunAsync(CoreDispatcherPriority.High,
        () =>
        {
           // Now that work is done, safely access UI components here to update them
        });
    }, period);



回答2:


You could use a System event background task, if you're looking for this to work while your app is not running.

It will wake up a minimum of every 15 minutes, even if your app is not running.



来源:https://stackoverflow.com/questions/14988128/refresh-data-in-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!