Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
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
Posted
Comments
Sinisa Hajnal 19-Sep-14 7:11am    
Check await keyword...not sure which version of C# it appeared in, but it brings asyncronous programming to mere mortals.

Also, you could start your work in timers as BackgroundWorkerThreads so timers block only for the time needed to start the threads.
Herman<T>.Instance 19-Sep-14 7:12am    
make the called methods in the timer tick event also asynchronous and update ui via events or Action<>
Sergey Alexandrovich Kryukov 19-Sep-14 10:42am    
1) Not clear; 2) Why using a timer event at all? Please, the ultimate goal.
—SA

C#
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;
                }
            }
        }
    }



Use this inside of the Timer control available in the Windows forms
 
Share this answer
 
Hey,
thank very much for your reply
 
Share this answer
 
Comments
TheRealSteveJudge 2-Dec-14 9:19am    
Do not abuse a solution for replying.
BillW33 2-Dec-14 11:11am    
You should not post a comment to a solution or an addition to your question as a "Solution". Either add a comment to a previous solution by pressing the "Have a Question or Comment?" button or update your question by using the "Improve question" button. I did not vote, but I thought you should know why this might be downvoted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900