如何异步运行计时器任务? [英] How to run the timer tasks asynchronously?

查看:78
本文介绍了如何异步运行计时器任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您正在使用C#和xaml开发Windows Phone 8应用。我以前的团队开发了一些代码,他们在定时器控制中使用了很多方法。当它正在更新时,所有方法都在调用并阻塞UI。有没有其他方法可以异步使用计时器,因此无法阻止UI。

提前致谢

Hi am developing a windows phone 8 app using C# and xaml. My previous team has developed some code, they have used many methods in timer control. when it is updating all the methods are calling and its blocking the UI. Is there any another way to use the timers asynchronously so that the UI can not be blocked.
Thanks in advance

推荐答案

public class Timer
    {
        public delegate void Timer_Tick(object sender, EventArgs e);
        public event Timer_Tick Tick;
        System.Threading.Thread TimerThread = null;

        public Int32 TimeoutFrequency {get;set;}
        public Boolean TimerWorking {get;set;}

        public void Start()
        {
            this.TimerWorking = true;
            this.TimerThread = new System.Threading.Thread(new System.Threading.ThreadStart(RunTimer));
            this.TimerThread.IsBackground = true;
            this.TimerThread.Start();
        }

        public void Stop()
        {
            this.TimerWorking = false;
            if (this.TimerThread.ThreadState == System.Threading.ThreadState.Running)
                this.TimerThread.Abort();
        }

        private void RunTimer()
        {
            Int32 inc = 0;
            while (this.TimerWorking)
            {
                inc++;
                System.Threading.Thread.Sleep(1000);
                if (inc >= this.TimeoutFrequency)
                {
                    Tick(this, new EventArgs());
                    inc = 0;
                }
            }
        }
    }







在Windows窗体中提供的Timer控件内部使用此




Use this inside of the Timer control available in the Windows forms


嘿,

非常感谢您的回复
Hey,
thank very much for your reply


这篇关于如何异步运行计时器任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆