c# - Timer WPF -the function of timers doesnt work on time -


public partial class window1 : window {     private dispatchertimer timer;   // timer       public window1()     {         initializecomponent();     }      private void window_loaded(object sender, routedeventargs e)     {         timer = new dispatchertimer();         timer.interval = timespan.fromseconds(1);         timer.tick += new eventhandler(timer_tick);           timer.start();         int b;//if arrives here instead of function timer_tick!!!!!!!     }      void timer_tick(object sender, eventargs e)     {         // function     } } 

the timer function starts after finishes current function. instead of starting when times starts, function starts after "int b" line!!

please read msdn dispatchertimer, states explicitly added dispatcher queue.

the dispatchertimer reevaluated @ top of every dispatcher loop. timers not guaranteed execute when time interval occurs, guaranteed not execute before time interval occurs. because dispatchertimer operations placed on dispatcher queue other operations. when dispatchertimer operation executes dependent on other jobs in queue , priorities.

dispatchertimer msdn

you might try system.timers.timer instead.

system.timers.timer


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -