我正在使用C#和xaml构建Windows应用商店应用程序。我需要在一定的时间间隔后刷新数据(从服务器获取新数据)。我使用ThreadPoolTimer定期执行我的刷新功能,如下所示:函数完成其任务后定期执行函数
TimeSpan period = TimeSpan.FromMinutes(15);
ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async(source)=> {
n++;
Debug.WriteLine("hello" + n);
await dp.RefreshAsync(); //Function to refresh the data
await Dispatcher.RunAsync(CoreDispatcherPriority.High,
() =>
{
bv.Text = "timer thread" + n;
});
}, period);
这是正常工作。唯一的问题是如果刷新函数在其下一个实例提交给线程池之前未完成。是否有某种方式来指定其执行之间的差距。
步骤1:刷新功能执行(花费的任何时间量)
步骤2:刷新功能完成其执行
步骤3:间隙为15分钟然后转到步骤1
刷新功能执行。执行完15分钟后,再次执行。