What is the best way to implement a timer? A code sample would be great! For this question, "best" is defined as most reliable (least number of misfires) and precise. If I specify an interval of 15 seconds, I want the target method invoked every 15 seconds, not every 10 - 20 seconds. On the other hand, I don't need nanosecond accuracy. In this example, it would be acceptable for the method to fire every 14.51 - 15.49 seconds.
72625
Just this - How do you add a timer to a C# console application? It would be great if you could supply some example coding.
41693
What's the proper way to execute a function every 3 months? Watchdog variables don't seem to be available after a certain time. I could create another table to insert the execute function date to compare, but it doesn't seem to be the best approach.  The functionality of such functions are:   Send an eMail Tag an inactive user
47907
Myself and a colleague have a dispute about which of the following is more elegant. I won't say who's who, so it is impartial. Which is more elegant?  public function set hitZone(target:DisplayObject):void         {             if(_hitZone != target)             {                 _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);                 _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);                 _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);                  _hitZone = target;                  _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);                 _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);                 _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);             }         }   ...or...  public function set hitZone(target:DisplayObject):void         {             if(_hitZone == target)return;              _hitZone.removeEventListener(MouseEvent.ROLL_OVER, onBtOver);             _hitZone.removeEventListener(MouseEvent.ROLL_OUT, onBtOut);             _hitZone.removeEventListener(MouseEvent.MOUSE_DOWN, onBtDown);              _hitZone = target;              _hitZone.addEventListener(MouseEvent.ROLL_OVER, onBtOver, false, 0, true);             _hitZone.addEventListener(MouseEvent.ROLL_OUT, onBtOut, false, 0, true);             _hitZone.addEventListener(MouseEvent.MOUSE_DOWN, onBtDown, false, 0, true);          }
58449
Imagine you want to animate some object on a WinForm. You setup a timer to update the state or model, and override the paint event of the Form. But from there, what's the best way to continually repaint the Form for the animation?   Invalidate the Form as soon as you are done drawing? Setup a second timer and invalidate the form on a regular interval? Perhaps there is a common pattern for this thing? Are there any useful .NET classes to help out?   Each time I need to do this I discover a new method with a new drawback. What are the experiences and recommendations from the SO community?
100557
I want a FFmpeg seeking command that fast and accurate. I found .  The solution is that we apply -ss for both input (fast seeking) and output (accurate seeking). But: If the input seeking is not accurate, how can we be sure that the seeking position is accurate?    For example: If we wanted to seek to 00:03:00, the command is something like:  ffmpeg -ss 00:02:30 -i &lt;INPUT&gt; ... -ss 00:00:30 &lt;OUTPUT&gt;   The first -ss will seek to somewhere else, not 00:02:30, say 00:02:31. And after applying the second seek, the final result would be 00:03:01- not what we want. Is that correct?  Where does the first -ss seek to? Does it seek to the keyframe that is closest to 00:02:30?  If so, here is my thought—correct me if I'm wrong: after first seeking, we get the timestamp of the result (in this example: 00:02:31), then we apply second seeking with appropriate time, in this case 00:00:29.  Question is: How do we get time stamp of the first seek result?
84304
I'm looking for a turn of phrase to describe a situation where the powers that be wish to continue making small improvements to a process which, due to deep-rooted flaws, will never be close to accurate.  I'm working on a process for estimating the states of business objects, and there's interest in fixing some edge cases which have been noticed.  This could be worthwhile, but because of missing data we're already making heroic guesses.  So if implemented, we might get from 80.0 to 80.1% accuracy.  If we were at 99.5%, I'd be happy to get it to 99.6%, but in the current situation it doesn't seem productive.  We have some CS people on E.SE; what would you call it when someone wants to optimize a two-second process to load data, though once loaded it will be chewed on by an inefficient algorithm for hours?  Something like "bailing the ocean" or "fixing a leaky faucet in a burning building" is close to what I have in mind.  There's also "rearranging deck chairs on the Titanic," but that carries an implication that the changes are wholly useless, and that events are coming to a head.
22644
I have a console app in which I want to give the user x seconds to respond to the prompt.  If no input is made after a certain period of time, program logic should continue.  We assume a timeout means empty response.  What is the most straightforward way of approaching this?
30155
How to, in C# round any value to 10 interval? For example, if I have 11, I want it to return 10, if I have 136, then I want it to return 140.   I can easily do it by hand  return ((int)(number / 10)) * 10;   But I am looking for an builtin algorithm to do this job, something like Math.Round(). The reason why I won't want to do by hand is that I don't want to write same or similar piece of code all over my projects, even for something as simple as the above.
2127
I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event?   I want the user to be able to stop the repeated refresh of data.
11635
Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?  for example:  start = time.clock() ... do something elapsed = (time.clock() - start)   vs.  start = time.time() ... do something elapsed = (time.time() - start)
27536
I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nanoseconds. As the API is a C++ class/function, I am using the timer.h to calculate the same:   #include &lt;ctime&gt;   #include &lt;iostream&gt;    using namespace std;    int main(int argc, char** argv) {        clock_t start;       double diff;       start = clock();       diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;       cout&lt;&lt;&quot;printf: &quot;&lt;&lt; diff &lt;&lt;'\n';        return 0;   }  The above code gives the time in seconds. How do I get the same in nano seconds and with more precision?
106949
I'd love some other opinions on what's more efficient in this code. Basically in the following code, there's a setInterval loop and I need 4 requirements to be true before the code runs in the loop. So in v.1 I wrote an if statement checking all 4. Worked fine.  Then I switched to just using try/catch, with the code I want to execute sitting in try{}. The logic was that during each loop, an exception would be generated but suppressed for each invalid condition. On the final loop where all conditions are true, the code executes and clears the interval.  Either works. I like the try/catch method because there's less conditional code that I need to write and worry about breaking. But I'm worried that try/catch is really inefficient, especially in a setInterval() loop hitting at 100ms. What are some opinions of other bright minds here on SO?  Try/Catch  var intvar = setInterval(function(){ try{         clearInterval(intvar);      jQuery('#'+nav[pageid].t1+'&gt;a').replaceWith(jQuery('&lt;span&gt;'+jQuery('#'+nav[pageid].t1+'&gt;a').text()+'&lt;/span&gt;'));      //set display classes for nav     jQuery('#'+nav[pageid].t1).addClass('selected').find('#'+nav[pageid].t2).addClass('subselect'); //topnav     jQuery('#'+nav[pageid].t3).addClass('selected').find('#'+nav[pageid].t4).addClass('subselect'); //leftnav }catch(err){} },100);   IF Block  var intvar = setInterval(function(){  if(typeof jQuery == 'function' &amp;&amp; typeof nav == 'object' &amp;&amp; typeof pageid != 'undefined' &amp;&amp; typeof document.getElementById('leftnav') == 'object'){     clearInterval(intvar);     jQuery('#'+nav[pageid].t1+'&gt;a').replaceWith(jQuery('&lt;span&gt;'+jQuery('#'+nav[pageid].t1+'&gt;a').text()+'&lt;/span&gt;'));      //set display classes for nav     jQuery('#'+nav[pageid].t1).addClass('selected').find('#'+nav[pageid].t2).addClass('subselect'); //topnav     jQuery('#'+nav[pageid].t3).addClass('selected').find('#'+nav[pageid].t4).addClass('subselect'); //leftnav }  },100);
70961
I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.  , but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.  The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program.  I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.  Thanks in advance.
42496
I have a method which should be delayed running for a specified amount of time.  Should I use  Thread thread = new Thread(() =&gt; {     Thread.Sleep(millisecond);     action(); }); thread.IsBackground = true; thread.Start();   Or  Timer timer = new Timer(o =&gt; action(), null, millisecond, -1);   I had read some  about using Thread.Sleep is bad design. But I don't really understand why.  But for using Timer, Timer has dispose method. Since the execution is delayed, I don't know how to dispose Timer. Do you have any suggestions?  Or if you have alternative codes for delayed execution are also appreciate.
56696
I find I always have trouble scaling the amount of time the eggs should be boiled, cooled, etc. for different-sized batches of hard-boiled eggs.  I've heard a variety of "folk lore"-type rules for how it should be done, but what's really the right way?
915
How do you write (and run) a correct micro-benchmark in Java?  I'm looking for some code samples and comments illustrating various things to think about.  Example: Should the benchmark measure time/iteration or iterations/time, and why?  Related:
33914
I was coding a physics simulation, and noticed that I was using discrete time. That is, there was an update mechanism advancing the simulation for a fixed amount of time repeatedly, emulating a changing system.  I though that was interesting, and now believe the real world must behave just like my program does. It is actually advancing forward in tiny but discrete time intervals?