在函数完成其任务后定期执行函数

Ank*_*bey 6 c# threadpool visual-studio-2012 windows-store-apps

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

这工作正常.唯一的问题是如果在将下一个实例提交给线程池之前刷新函数没有完成该怎么办.有没有办法指定执行之间的差距.

第1步:执行刷新功能(花费任何时间)

第2步:刷新功能完成其执行

步骤3:间隙15分钟然后转到步骤1

刷新功能执行.执行结束后15分钟,它再次执行.

byt*_*ire 6

的AutoResetEvent会解决这个问题.声明一个类级AutoResetEvent实例.

AutoResetEvent _refreshWaiter = new AutoResetEvent(true);
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:1.等待它直到它被发出信号,并且2.将它的引用作为参数传递给RefreshAsync方法.

TimeSpan period = TimeSpan.FromMinutes(15); 
   ThreadPoolTimer PeriodicTimer =  ThreadPoolTimer.CreatePeriodicTimer(async(source)=> {  
   // 1. wait till signaled. execution will block here till _refreshWaiter.Set() is called.
   _refreshWaiter.WaitOne();
   n++; 
   Debug.WriteLine("hello" + n);
   // 2. pass _refreshWaiter reference as an argument
   await dp.RefreshAsync(_refreshWaiter); //Function to refresh the data
   await Dispatcher.RunAsync(CoreDispatcherPriority.High,
                () =>
                {
                    bv.Text = "timer thread" + n;

                });

        }, period);
Run Code Online (Sandbox Code Playgroud)

最后,在dp.RefreshAsync方法结束时调用_refreshWaiter.Set();,如果已经过了15秒,则可以调用下一个RefreshAsync.请注意,如果RefreshAsync方法花费的时间少于15分钟,则执行正常进行.