Cancellable threading.Timer in Python

You would call the cancel method after you start the timer: import time import threading def hello(): print “hello, world” time.sleep(2) t = threading.Timer(3.0, hello) t.start() var=”something” if var == ‘something’: t.cancel() You might consider using a while-loop on a Thread, instead of using a Timer. Here is an example appropriated from Nikolaus Gradwohl’s answer … Read more

System.Timers.Timer/Threading.Timer vs Thread with WhileLoop + Thread.Sleep For Periodic Tasks

System.Threading.Timer has my vote. System.Timers.Timer is meant for use in server-based (your code is running as a server/service on a host machine rather than being run by a user) timer functionality. A Thread with a While loop and Thread.Sleep command is truly a bad idea given the existance of more robust Timer mecahnisms in .NET.

Get timer ticks in Python

In the time module, there are two timing functions: time and clock. time gives you “wall” time, if this is what you care about. However, the python docs say that clock should be used for benchmarking. Note that clock behaves different in separate systems: on MS Windows, it uses the Win32 function QueryPerformanceCounter(), with “resolution … Read more

Most accurate timer in .NET?

I also have witten a class which is accurate to 1ms. I took Hans Passant’s code from forum https://social.msdn.microsoft.com/Forums/en-US/6cd5d9e3-e01a-49c4-9976-6c6a2f16ad57/1-millisecond-timer and wrapped it in a class for ease of use in your Form. You can easily set up multiple timers if you want. In the example code below I have used 2 timers. I have tested … Read more

javascript timer loop

Instead of setTimeout, consider using setInterval. It will repeat automatically until you clear the interval. setInterval(myMethod, 5000); function myMethod( ) { //this will repeat every 5 seconds //you can reset counter here }

.NET, event every minute (on the minute). Is a timer the best option?

Building on the answer from aquinas which can drift and which doesn’t tick exactly on the minute just within one second of the minute: static System.Timers.Timer t; static void Main(string[] args) { t = new System.Timers.Timer(); t.AutoReset = false; t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); t.Interval = GetInterval(); t.Start(); Console.ReadLine(); } static double GetInterval() { DateTime now … Read more

Jquery/Ajax call with timer

If you want to set something on a timer, you can use JavaScript’s setTimeout or setInterval methods: setTimeout ( expression, timeout ); setInterval ( expression, interval ); Where expression is a function and timeout and interval are integers in milliseconds. setTimeout runs the timer once and runs the expression once whereas setInterval will run the … Read more

Android Timer schedule vs scheduleAtFixedRate

The difference is best explained by this non-Android documentation: Fixed-rate timers (scheduleAtFixedRate()) are based on the starting time (so each iteration will execute at startTime + iterationNumber * delayTime). In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such … Read more