Are c# timers naturally multithreaded?

后端 未结 3 1994
时光说笑
时光说笑 2020-12-19 22:21

If I have two system timers firing events at 10 and 20s respectively, are the calls to the event functions multithreaded? In the scenario below, I have timers that fire even

相关标签:
3条回答
  • 2020-12-19 23:04

    The CLR uses a dedicated thread to keep track of active System.Timers.Timer and System.Threading.Timer timers. The Elapsed event is raised on another thread pulled from the threadpool.

    So yes, they keep ticking without affecting each other. You have to be very careful, it is quite possible for your Elapsed event handler to be called again while it is still executing. Which happens when it takes longer than the interval. Or worse, when the machine is heavily loaded or you have a lot of active threadpool threads. This can cause very hard to diagnose failure if your event handler isn't thread-safe. It almost never is. Setting the timer's AutoReset property to false is a simple way to avoid this problem.

    0 讨论(0)
  • 2020-12-19 23:07

    Looks like it won't block if you use System.Threading.Timer or invoke System.Timers.Timer without a SynchronizingObject.

    Similar question: Do C# Timers elapse on a separate thread?

    0 讨论(0)
  • 2020-12-19 23:12

    From the System.Timers.Timer documentation:

    The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.

    So yes, it is.

    Futhermore, from the System.Timers.Timer.SynchronizingObject property documentation:

    When SynchronizingObject is null, the method that handles the Elapsed event is called on a thread from the system-thread pool. For more information on system-thread pools, see ThreadPool.

    So the events are raised on thread pool threads unless SynchronizingObject is set.

    0 讨论(0)
提交回复
热议问题