Non-reentrant C# timer

Posted by Oak on Stack Overflow See other posts from Stack Overflow or by Oak
Published on 2010-06-06T19:45:29Z Indexed on 2010/06/06 19:52 UTC
Read the original article Hit count: 438

Filed under:
|
|

I'm trying to invoke a method f() every t time, but if the previous invocation of f() has not finished yet, wait until it's finished.

I've read a bit about the available timers (this is a useful link) but couldn't find any good way of doing what I want, save for manually writing it all. Any help about how to achieve this will be appreciated, though I fear I might not be able to find a simple solution using timers.

To clarify, if x is one second, and f() runs the arbitrary durations I've written below, then:

Step  Operation    Time taken
1     wait         1s
2     f()          0.6s
3     wait         0.4s (because f already took 0.6 seconds)
4     f()          10s
5     wait         0s (we're late)
6     f()          0.3s
7     wait         0.7s (we can disregard the debt from step 4)

Notice that the nature of this timer is that f() will not need to be safe regarding re-entrance, and a thread pool of size 1 is enough here.

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading