Badr
Badr

Reputation: 10648

how to get a real time clock in 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;

Upvotes: 11

Views: 12832

Answers (7)

Prabhat Kumar
Prabhat Kumar

Reputation: 29

If you have no internet access and you want real time for timed trial license of software then you can use USB dongle with Real time clock.

Its time calculation is driven by an internal clock which is battery-driven. You can access the time from USB dongle using API functions which come along with these types of dongles.

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

Upvotes: 0

Chris
Chris

Reputation: 1

If you want a good time mesurement I think csharp as an high level language might not be a good idea. you could assemble a simple program that taps to RTC and then tie it to your program via a library. That is literally the best possible scenario you can get whith no Object overhang, but it requires some low level know how to put together.

Upvotes: 0

Chris Schmich
Chris Schmich

Reputation: 29456

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).

Upvotes: 7

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

For a high resolution measurements you could 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;
    }
}

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 837936

The most precise timer available in .NET is StopWatch.

Upvotes: 4

Dean Harding
Dean Harding

Reputation: 72638

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

Upvotes: 16

Related Questions