"thread-safe execution using system.threading.timer and monitor" Code Answer


lots of ways to deal with this. a simple way is to just not make the timer periodic, make it a one shot by only setting the duetime argument. then re-enable the timer in the callback in a finally block. that guarantees that the callback cannot run concurrently.

this is of course makes the interval variable by the execution time of the callback. if that's not desirable and the callback only occasionally takes longer than the timer period then a simple lock will get the job done. yet another strategy is monitor.tryenter and just give up on the callback if it returns false. none of these are particularly superior, pick what you like best.

By Ines Tlili on March 23 2022

Answers related to “thread-safe execution using system.threading.timer and monitor”

Only authorized users can answer the Search term. Please sign in first, or register a free account.