The System.Diagnostics.Stopwatch class in .Net, used to accurately measure elapsed time.
Questions tagged [stopwatch]
837 questions
31
votes
2 answers
Are Stopwatch.ElapsedTicks and Stopwatch.Elapsed.Ticks always the same?
What does ElapsedTicks and Elapsed.Ticks in the Stopwatch class mean? When could the meaning be different than intended?

Fredou
- 19,848
- 10
- 58
- 113
30
votes
3 answers
Elapsed time on a timer?
I have a really simple bit of code I use to test the response from proxy servers, I want to be able to start a timer and stop it and get the elapsed time it took for those to happen. However, I don't believe the code I have below is what I'm looking…
user222427
23
votes
1 answer
How to get actual request execution time
Given the following middleware:
public class RequestDurationMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public RequestDurationMiddleware(RequestDelegate next,…

natenho
- 5,231
- 4
- 27
- 52
22
votes
4 answers
System.Diagnostics.Stopwatch returns negative numbers in Elapsed... properties
Is it normal behaviour that Stopwatch can return negative values? Code sample below can be used to reproduce it.
while (true)
{
Stopwatch sw = new Stopwatch();
sw.Start();
sw.Stop();
if…

Vadym Stetsiak
- 1,974
- 18
- 22
21
votes
5 answers
How accurate is Thread.Sleep(TimeSpan)?
I've come across a unit test that is failing intermittently because the time elapsed isn't what I expect it to be.
An example of what this test looks like is:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
TimeSpan oneSecond = new…

mezoid
- 28,090
- 37
- 107
- 148
17
votes
3 answers
Can the .NET Stopwatch class be THIS terrible?
I'm making an application that needs some pretty tight timing, and the Stopwatch class is the perfect solution. However, I noticed sometimes, when running on a small panel PC, the Stopwatch values were way off. I added some debug printouts that…

BabaBooey
- 1,552
- 3
- 21
- 38
17
votes
2 answers
StopWatch vs Timer - When to Use
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…
user4835507
16
votes
4 answers
Is Stopwatch.ElapsedTicks threadsafe?
If I have a shared System.Diagnostics.Stopwatch instance, can multiple threads call shared.ElapsedTicks in a safe manner and get accurate results?
Is there any difference in terms of thread-safety/accuracy between using a shared instance of…

Rob
- 4,327
- 6
- 29
- 55
16
votes
3 answers
Raise event in high resolution interval/timer
I want to use the highest possible resolution timer using c#. For example, I want to raise an event every 11 ticks (I've heard that tick is the highest possible counter in pc). I tried timer and found that minimum elapsed time is in milliseconds. I…

Syaiful Nizam Yahya
- 4,196
- 11
- 51
- 71
16
votes
8 answers
How to transform milliseconds into seconds .?
I have to transform my Stopwatch "Variable" into seconds ?
Stopwatch czasAlg = new Stopwatch();
czasAlg.Start();
//Do semothing
czasAlg.Stop();
Decimal Tn = czasAlg.ElapsedMilliseconds/(decimal)n;

Konrad
- 726
- 4
- 17
- 33
15
votes
2 answers
Is .NET Stopwatch standby/sleep/hibernate aware?
Does System.Diagnostics.Stopwatch count time during computer stand by?

Evgenyt
- 10,201
- 12
- 40
- 44
14
votes
2 answers
Multicore and thread aware .Net stopwatch?
As we all know the stopwatch might have issues in a multi-threaded async application that will be running on all cores.
On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the…

user316
- 309
- 1
- 8
14
votes
3 answers
Average value of list
I want to make code that will on the first click start rotorSpeed stopwatch then on the second click add rotorSpeed.ElapsedMilliseconds to list. On the second click resets stopwatch and starts timing again, then on the third click again adds…

rltcounter221
- 183
- 1
- 1
- 10
14
votes
1 answer
C# Stopwatch shows incorrect time
I have seen other user posts which show Stopwatch measuring time spent in "Thread.Sleep(5000)" to be around 5000ms.
But my program produces the following results
for (int i = 0; i < 20; ++i)
{
Stopwatch sw = Stopwatch.StartNew();
DateTime…

Alexander Demuro
- 253
- 2
- 7
14
votes
3 answers
Is there already a StopWatch class for android and why doesn't my implementation work?
Lately I saw http://developer.android.com/reference/android/os/CountDownTimer.html and wondered if there is an respective class for stop watches since I want to tell the user of my App how long he's already trying to solve the puzzle. I mean it…

principal-ideal-domain
- 3,998
- 8
- 36
- 73