Update a Label with a Swing Timer

I don’t really understand your question why you are using the Random, but here are some observations: I want to update a JLabel with the countdown, every second. Then you need to set the Timer to fire every second. So the parameter to the Timer is 1000, not some random number. Also, in your actionPerformed() … Read more

iOS Run Code Once a Day

Here’s the situation regarding background execution and notifications and timers etc. in relation to an app scheduling some activity to happen periodically. An app cannot execute in the background unless: It requests extra time from the OS to do so. This is done using beginBackgroundTaskWithExpirationHandler. It is not specified (intentionally) by Apple how long this … Read more

Run a java function after a specific number of seconds

new java.util.Timer().schedule( new java.util.TimerTask() { @Override public void run() { // your code here } }, 5000 ); EDIT: javadoc says: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer’s task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can … Read more

Best timing method in C?

I think this should work: #include <time.h> clock_t start = clock(), diff; ProcessIntenseFunction(); diff = clock() – start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf(“Time taken %d seconds %d milliseconds”, msec/1000, msec%1000);

Is there a Task based replacement for System.Threading.Timer?

It depends on 4.5, but this works. public class PeriodicTask { public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken) { while(!cancellationToken.IsCancellationRequested) { await Task.Delay(period, cancellationToken); if (!cancellationToken.IsCancellationRequested) action(); } } public static Task Run(Action action, TimeSpan period) { return Run(action, period, CancellationToken.None); } } Obviously you could add a generic version that takes … Read more

How to repeatedly execute a function every x seconds?

If your program doesn’t have a event loop already, use the sched module, which implements a general purpose event scheduler. import sched, time s = sched.scheduler(time.time, time.sleep) def do_something(sc): print(“Doing stuff…”) # do your stuff sc.enter(60, 1, do_something, (sc,)) s.enter(60, 1, do_something, (s,)) s.run() If you’re already using an event loop library like asyncio, trio, … Read more

Android timer updating a textview (UI)

protected static void startTimer() { isTimerRunning = true; timer.scheduleAtFixedRate(new TimerTask() { public void run() { elapsedTime += 1; //increase every sec mHandler.obtainMessage(1).sendToTarget(); } }, 0, 1000); } public Handler mHandler = new Handler() { public void handleMessage(Message msg) { StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview } }; Above code will work… Note: Handlers must be created … Read more

Difference between std::system_clock and std::steady_clock?

From N3376: 20.11.7.1 [time.clock.system]/1: Objects of class system_clock represent wall clock time from the system-wide realtime clock. 20.11.7.2 [time.clock.steady]/1: Objects of class steady_clock represent clocks for which values of time_point never decrease as physical time advances and for which values of time_point advance at a steady rate relative to real time. That is, the clock … Read more