Windows Phone 8的计时器
我希望以1分钟左右的间隔执行一个函数。 如何在Windows Phone 8中实现此目的。
我不是在找背景代理人 。 该应用程序将在前台运行。 我有什么选择?
您可以使用DispatcherTimer类
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 }
参考:( https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer ( v=vs.110 ) .aspx )
试试这个
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 }
您还可以使用ThreadPoolTimer
:
TimeSpan period = TimeSpan.FromSeconds(60); ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) => { // TODO: Work Deployment.Current.Dispatcher.BeginInvoke(() => { // UI update }); }, period);
一种选择可能是使用DispatcherTimer 。
只需在Tick事件上注册回调即可。
上述就是C#学习教程:Windows Phone 8的计时器分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/936165.html