17

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 facing this dilemma:

See, I am coding a simple application. I want it to show the elapsed time (hh:mm:ss format or something). But also, to be able to "speed up" or "slow down" its time intervals (i.e. speed up so that a minute in real time equals an hour in the app).

For example, in Youtube videos (* let's not consider the fact that we can jump to specific parts of the vid *), we see the actual time spent in watching that video on the bottom left corner of the screen, but through navigating in the options menu, we are able to speed the video up or down.

Youtube (2015) video player control bar

And we can actually see that the time gets updated in a manner that agrees with the speed factor (like, if you choose twice the speed, the timer below gets updated twice faster than normal), and you can change this speed rate whenever you want.

enter image description here

This is what I'm kinda after. Something like how Youtube videos measure the time elapsed and the fact that they can change the time intervals. So, which of the two do you think I should choose? Timer or StopWatch?

I'm just coding a Windows Form Application, by the way. I'm simulating something and I want the user to be able to speed up whenever he or she wishes to. Simple as this may be, I wish to implement a proper approach.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 1
    possible duplicate of [How would I go about implementing a stopwatch with different speeds?](http://stackoverflow.com/questions/2857210/how-would-i-go-about-implementing-a-stopwatch-with-different-speeds) – Matías Fidemraizer Jul 08 '15 at 12:53
  • 3
    A timer is useless to measure elapsed time, it is only good enough to get that Label updated. Use Environment.TickCount, DateTime.UtcNow or Stopwatch. The first two are very accurate over long periods, the latter is good for short periods. – Hans Passant Jul 08 '15 at 12:59
  • @HansPassant: +1, I always use StopWatch because of exactly the points you bring out. – code4life Jul 08 '15 at 13:01

2 Answers2

23

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.

Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

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.

Community
  • 1
  • 1
SpaceSteak
  • 224
  • 2
  • 15
  • 1
    If it has been asked and answered many times, you shouldn't answer and wait for others to close it... – Matías Fidemraizer Jul 08 '15 at 12:52
  • I've added a close vote, BTW, you should know that your "answer" should be a comment... – Matías Fidemraizer Jul 08 '15 at 12:53
  • thanks for the heads up @MatíasFidemraizer ... I still can't vote to close questions yet, soyou're right I should just have posted a comment. – SpaceSteak Jul 08 '15 at 12:55
  • On second thought, I don't think that page has fully explained to me when to use which. :/ Also, this question has just been up for JUST ABOUT 12 minutes while I'm writing this comment. And I am looking at the other page linked. My apologies, then, if you find that necessary. :/ –  Jul 08 '15 at 12:59
  • @SpaceSteak No problem, I added the close vote myself ;) – Matías Fidemraizer Jul 08 '15 at 14:35