StopWatch vs Timer - When to Use

前端 未结 2 1323
无人共我
无人共我 2021-02-13 14:16

Forgive me for this question, but I can\'t seem to find a good source of when to use which. Would be happy if you can explain it in simple terms.

Furthermore, I am faci

2条回答
  •  失恋的感觉
    2021-02-13 15:10

    As far as I know the main differences are:

    Timer

    1. Timer is just a simple scheduler that runs some operation/method once in a while
    2. It executes method in a separate thread. This prevents blocking of the main thread

    Timer is good when we need to execute some task in certain time interval without blocking anything.

    Stopwatch

    1. Stopwatch by default runs on the same thread
    2. It counts time and returns TimeSpan struct that can be useful in case when we need some additional information

    Stopwatch is good when we need to watch the time and get some additional information about how much elapsed processor ticks does the method take etc.

提交回复
热议问题