开发者

how to get a real time clock in C#?

i want a real time clock with precision of mi开发者_C百科croseconds how can i do so?

is there any real time clock other than stopwatch;


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


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


The most precise timer available in .NET is StopWatch.


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;
    }
}


check this http://www.c-sharpcorner.com/UploadFile/kapilsoni88/Digital_Click08142008142713PM/Digital_Click.aspx and also check this http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜