Questions tagged [timer]

Timer is a component that has the functionality to trigger a user defined action at regular intervals as configured by the user.

Timer is a component that has the functionality to trigger a user defined action at regular intervals as configured by the user.

Some timers (such as Java Swing timer, WPF DispatcherTimer, WinForms Timer) are also capable of firing they events in GUI thread so there is no need to switch into this thread in order to update the components. Other timers do not have this capability and are more optimized to handle big number of scheduled events.

Depending on how does the scheduler behaves if the new even should be fired when the current event has not yet been fully processed, the timer may be coalescing or non coalescing. A coalescing timer reduces multiple pending events to a single event, reducing the number of events to handle. A non-coalescing timer fires pending overtime events in rapid succession with no delay between them, in order to catch up.

19566 questions
44
votes
7 answers

Time elapse computation in milliseconds C#

I need to time the execution of a code sequence written in C#. Using DateTime.Now I get incorrect values for the millisecond field. For example: int start_time, elapsed_time; start_time = DateTime.Now.Millisecond; for(int i = 0; i < N_ITER;…
Sebi
  • 4,262
  • 13
  • 60
  • 116
44
votes
15 answers

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

I want to do stuff every minute on the minute (by the clock) in a windows forms app using c#. I'm just wondering whats the best way to go about it ? I could use a timer and set its interval to 60000, but to get it to run on the minute, I would have…
Kieran
43
votes
9 answers

How I can run my TimerTask everyday 2 PM?

I want to execute a job everyday 2PM. Which method of java.util.Timer I can use to schedule my job? After 2Hrs, run it will stop the job and reschedule for next day 2PM.
BOSS
  • 2,931
  • 8
  • 28
  • 53
43
votes
3 answers

DispatcherTimer vs a regular Timer in WPF app for a task scheduler

Please, explain the difference between "DispatcherTimer" and "a regular Timer" that @Kent Boogaart meant for using in a multithreading WPF app as a task sheduler in this topic: Advice needed for multi-threading strategy for WPF application in the…
rem
  • 16,745
  • 37
  • 112
  • 180
42
votes
2 answers

python theading.Timer: how to pass argument to the callback?

I tried this code: import threading def hello(arg, kargs): print(arg) t = threading.Timer(2, hello, "bb") t.start() while 1: pass The output is just b. How can I pass arguments to the callback properly? If I remove the kargs parameter…
Bin Chen
  • 61,507
  • 53
  • 142
  • 183
42
votes
8 answers

How to get time.Tick to tick immediately

I have a loop that iterates until a job is up and running: ticker := time.NewTicker(time.Second * 2) defer ticker.Stop() started := time.Now() for now := range ticker.C { job, err := client.Job(jobID) switch err.(type) { case…
Xavi
  • 20,111
  • 14
  • 72
  • 63
41
votes
5 answers

How to trigger an action after x seconds in SwiftUI?

I have been trying to accomplish two main goals that I'm having a headache with. Sorry if it's a simple fix, I am a still bit new to Swift/SwiftUI. Trigger an action after a certain time has elapsed. Trigger an @State to change value based on how…
Anthney
  • 735
  • 1
  • 7
  • 12
41
votes
6 answers

VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds

I have a need to run a piece of code every 120 seconds. I am looking for an easy way to do this in VBA. I know that it would be possible to get the timer value from the Auto_Open event to prevent having to use a magic number, but I can't quite get…
FinancialRadDeveloper
  • 984
  • 4
  • 13
  • 28
40
votes
5 answers

How do you time a function in Go and return its runtime in milliseconds?

How do you time a function in Go and return its runtime in milliseconds?
The Puma
  • 1,352
  • 2
  • 14
  • 27
40
votes
4 answers

How do I get the Exception that happens in Timer Elapsed event?

I'm working with Windows Forms application and hava a manager class that uses System.Timers.Timer to periodly check data from database. How do I get the Exception that occurs in timer Elapsed eventhandler delivered into main application? If I'm…
Clack
  • 945
  • 5
  • 11
  • 18
40
votes
8 answers

Resettable Java Timer

I'd like to have a java.utils.Timer with a resettable time in java.I need to set a once off event to occur in X seconds. If nothing happens in between the time the timer was created and X seconds, then the event occurs as normal. If, however,…
Lehane
  • 47,588
  • 14
  • 53
  • 53
40
votes
2 answers

Time-Limited Input?

What I would like to be able to do is ask a user a question using input. For example: print('some scenario') prompt = input("You have 10 seconds to choose the correct answer...\n") and then if the time elapses print something like print('Sorry,…
cloud311
  • 3,101
  • 5
  • 20
  • 20
39
votes
1 answer

How Do I write a Timer in Objective-C?

I am trying to make a stop watch with NSTimer. I gave the following code: nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO]; and it is not working in milliseconds. It takes…
new_programmer
  • 840
  • 3
  • 12
  • 22
39
votes
3 answers

Is it OK to call clearInterval inside a setInterval handler?

I have a piece of Javascript that checks for a condition (via an AJAX call) every n seconds. If that condition is true, it stops checking. I have implemented it in the following way: var stopTimer; var timerId = setInterval(function() { /* Make…
Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43
39
votes
3 answers

High-Performance Timer vs StopWatch

Does anyone know if the HiPerfTimer or the StopWatch class is better for benchmarking, and why?
Kane
  • 16,471
  • 11
  • 61
  • 86