StopWatch vs Timer - When to Use

让人想犯罪 __ 提交于 2019-12-03 11:11:47

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.

SpaceSteak

This has already been covered in a number of other questions including here. Basically, you can either have Stopwatch with a Speed factor then the result is your "elapsed time". A more complicated approach is to implement Timer and changing the Interval property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!