What if a timer can not finish all its works before the new cycle time arrives? What if a timer can not finish all its works before the new cycle time arrives? multithreading multithreading

What if a timer can not finish all its works before the new cycle time arrives?


If you're using System.Threading.Timer or System.Timers.Timer, the timer will tick again, starting a new thread. See https://stackoverflow.com/a/10442117/56778 for a way to avoid that problem.

If you're using System.Windows.Forms.Timer, then a new tick won't occur until the previous one is finished processing.


Put your code in a Monitor.TryEnter()

object timeCheck= new object();void Timer(){    Monitor.TryEnter(timeCheck)     {        //Code that might take too long         //...        Monitor.Exit();    }}


to prevent reentrancy, you might use a static boolean that tells wether the function is allready beeing executed. User a try/Catch/finally and set this boolean to false in the finally to ensure that the boolean does not remain false if you made a mistake in code or if the code failed.
For a faster timer, reentrancy should be prevented by using semaphore (mutex).