Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a real time clock in C#?

Tags:

c#

i want a real time clock with precision of microseconds how can i do so?

is there any real time clock other than stopwatch;

like image 205
Badr Avatar asked May 10 '10 05:05

Badr


2 Answers

You want System.Diagnostics.Stopwatch, which can measure time intervals accurately to the microsecond (depending on hardware limitations).

like image 61
Dean Harding Avatar answered Oct 20 '22 23:10

Dean Harding


System.Diagnostics.Stopwatch uses the QueryPerformanceCounter and QueryPerformanceFrequency Windows APIs. The resolution is not standard across machines, but generally it's on the order of microseconds (see this KB article for more info).

In Vista/Win7 there is also QueryProcessCycleTime and QueryThreadCycleTime which will give you the number of elapsed cycles for your process/thread. This is useful if you want to know only the active time for the process (e.g. time when you weren't switched-out).

like image 37
Chris Schmich Avatar answered Oct 20 '22 23:10

Chris Schmich