How to get real time clock in C #?

I need a real time clock accurate to microseconds, how can I do this?

is there a real time clock other than a stopwatch;

+10
c #
May 10 '10 at 5:46 a.m.
source share
7 answers

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

+15
May 10 '10 at 5:48 a.m.
source share

System.Diagnostics.Stopwatch uses the QueryPerformanceCounter and QueryPerformanceFrequency Windows API. Resolution is not standard on machines, but it is usually in the order of microseconds (see this article in KB for more information).

In Vista / Win7 there is also a QueryProcessCycleTime and QueryThreadCycleTime , which will give you the number of cycles passed for your process / thread. This is useful if you want to know only the active time for the process (for example, the time when you were not disconnected).

+7
May 10 '10 at 6:27
source share

The most accurate timer available in .NET is StopWatch .

+4
May 10 '10 at 5:48 a.m.
source share

For high resolution measurements, you can use the QueryPerformanceCounter function:

 class Program { [DllImport("kernel32.dll")] private static extern void QueryPerformanceCounter(ref long ticks); static void Main() { long startTicks = 0L; QueryPerformanceCounter(ref startTicks); // some task long endTicks = 0L; QueryPerformanceCounter(ref endTicks); long res = endTicks - startTicks; } } 
+3
May 10 '10 at 6:23 a.m.
source share

If you want to have a good time, I think csharp as a high-level language might not be a good idea. You can build a simple program that enters RTC and then binds it to your program through the library. This is literally the best possible scenario that you can get without opening the object, but this requires some low level of knowledge.

0
Feb 17 '14 at 22:19
source share

If you don’t have Internet access and want a real-time trial license for the software, then you can use a USB key with a real-time clock.

His time calculation is carried out by an internal clock, which is controlled by a battery. You can access time from a USB key using the API functions that come with these types of keys.

EX- http://www.senselock-europe.com/en/elite-el-rtc-dongle.html

0
Apr 15 '14 at 15:10
source share



All Articles