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
60
votes
1 answer

Difference between DispatchSourceTimer, Timer and asyncAfter?

I am struggling to understand the key differences between DispatchSourceTimer, Timer and asyncAfter (in my case for scheduling a task that needs to be ran every X seconds, although understanding the differences in timers can be useful to) (Or is…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
59
votes
5 answers

Pass async Callback to Timer constructor

I have async callback, which is passed into Timer(from System.Threading) constructor : private async Task HandleTimerCallback(object state) { if (timer == null) return; if (asynTaskCallback != null) { await…
demo
  • 6,038
  • 19
  • 75
  • 149
59
votes
11 answers

How do I measure how long a function is running?

I want to see how long a function is running. So I added a timer object on my form, and I came out with this code: private int counter = 0; // Inside button click I have: timer = new Timer(); timer.Tick += new…
sebastian.roibu
  • 2,579
  • 7
  • 37
  • 59
58
votes
9 answers

How to use the .NET Timer class to trigger an event at a specific time?

I would like to have an event triggered in my app which runs continuously during the day at a certain time, say at 4:00pm. I thought about running the timer every second and when the time is equal to 4:00pm run the event. That works. But I'm…
Behrooz Karjoo
  • 4,250
  • 10
  • 38
  • 48
58
votes
3 answers

Bash sleep in milliseconds

I need a timer which will work with milliseconds. I tried to use sleep 0.1 command in a script, but I see this error message: syntax error: invalid arithmetic operator (error token is ".1") When I run sleep 0.1 in terminal it works fine. Please help…
Noqrax
  • 1,049
  • 2
  • 8
  • 15
57
votes
6 answers

How do I calculate the elapsed time of an event in Java?

What's a simple/easy way to access the system clock using Java, so that I can calculate the elapsed time of an event?
kafuchau
  • 5,573
  • 7
  • 33
  • 38
56
votes
7 answers

How do I pass an object into a timer event?

Ok so I am using System.Timers.Timer in .Net 4 with C#. I have my timer object like so: var timer = new Timer {Interval = 123}; I have my Timer Elapsed event handler pointed at a method like so: timer.Elapsed += MyElapsedMethod; And my method…
JMK
  • 27,273
  • 52
  • 163
  • 280
56
votes
3 answers

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

Suppose we have a timer which runs every 10 minutes. What if the cycle of its processing takes more than 10 minutes. Does a new thread starts for that? Will it interrupt its current operation? What if a single object is mutated inside the…
Farshid
  • 5,134
  • 9
  • 59
  • 87
55
votes
4 answers

What is minimum millisecond value of setTimeout?

I would like to put var minValue = 0; if ( typeof callback == 'function' ) { setTimeout( callback, minValue ); } this code when I implement callback function with JavaScript. But I've found that modern browsers and some old browsers have…
jwchang
  • 10,584
  • 15
  • 58
  • 89
53
votes
12 answers

Best way to create a "run-once" time delayed function in C#

I am trying to create a function that takes in an Action and a Timeout, and executes the Action after the Timeout. The function is to be non-blocking. The function must be thread safe. I also really, really want to avoid Thread.Sleep(). So far,…
Chuu
  • 4,301
  • 2
  • 28
  • 54
53
votes
7 answers

How do I measure a time interval in C?

I would like to measure time in C, and I am having a tough time figuring it out, all I want is something like this: start a timer run a method stop the timer report the time taken (at least to micro accuracy) Any help would be appreciated. (I am…
naspinski
  • 34,020
  • 36
  • 111
  • 167
52
votes
12 answers

Android: CountDownTimer skips last onTick()!

Code: public class SMH extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); new CountDownTimer(10000,…
52
votes
9 answers

Android timer updating a textview (UI)

I'm using a timer to create a stop watch. The timer works by increasing a integer value. I want to then display this value in the activity by constantly updating a textview. Here's my code from the service where I try and update the activity's…
JDS
  • 16,388
  • 47
  • 161
  • 224
50
votes
4 answers

Using Swift 3 Stopping a scheduledTimer, Timer continue firing even if timer is nil

We call startTimer function to start a timer. When we wanted to stop it we call stopTimerTest function but after we called stopTimer function the timerTestAction keeps firing. To check the timer condition we used print and print in timerActionTest…
Hope
  • 2,096
  • 3
  • 23
  • 40
50
votes
9 answers

Execute function after 5 seconds in Android

I am new in android development and now my launcher activity show only 5 seconds and after that I want to check the user is logged in or not function and perform the actions. here is my code. @Override protected void onCreate(Bundle…
Jishad
  • 2,876
  • 8
  • 33
  • 62