36

Can I get the elapsed time since I have called Start on a stopwatch using ElapsedMilliseconds without calling Stop? I have searched a lot round the internet but only saw examples where ElapsedMilliseconds is called after Stop. Is this value filled on a call to Stop or is it always correct?

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176

1 Answers1

44

You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped. The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

— From https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch.elapsedmilliseconds#remarks

Pang
  • 9,564
  • 146
  • 81
  • 122
Rob G
  • 3,496
  • 1
  • 20
  • 29
  • I am seeing 'interesting' (to put it nicely) situations where `watch.Elapsed.Ticks != watch.ElapsedTicks` so there is weird stuff going on inside Stopwatch. Caveat emptor. Guess I should have suspected that since ElapsedTicks is a property not a method. While I'm complaining, Stopwatch is not reliable on multi-core machines. – Adam Dec 01 '13 at 18:14
  • 3
    The statement retrieves the time twice. If the stopwatch is running then of course it may change between those two retrievals. [if you are seeing this after stopping the watch, then I'll grant you "weird" applies. – Dale Wilson Dec 09 '13 at 16:45
  • 1
    @DaleWilson the two properties mean something different; see [this question](http://stackoverflow.com/questions/7432735/differences-in-elapsed-ticks-property-of-stopwatch) – mvo Jan 25 '14 at 16:12
  • 2
    If you call stopwatch.ElapsedMilliseconds twice in a row I doubt you'd be guaranteed the same result unless you had called .Stop() first. – user420667 Oct 16 '15 at 17:45