tags:

views:

177

answers:

3

Hello,

I need to format the day time using QueryPerformanceCounter Win32 API. The format, is: HH:mm:ss.ffffff , containing hours minuts seconds and microseconds. I need to use THIS function, because another process (written in C) is using this function and the purpose is using the same function in both places.

Thanks

+3  A: 

You should not use QueryPerformanceCounter to determine time of day. It can only be used to determine an elapsed interval with a very high resolution as it returns the number of ticks that passed since the computer was last restarted.

As such, at best, you may only determine how many hours, minutes, and seconds have passed since a previous reading of QueryPerformanceCounter which must not have happened too long in the past.

In order to convert from ticks to seconds you need to determine the frequency (using QueryPerformanceFrequency) of the ticks on the computer you're running the QueryPerformanceCounter function and then divide your reading by that frequency:

// obtain frequency
long freq;
QueryPerformanceFrequency(freq);

// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)

// .. do some work

// obatin your second reading
QueryPerformanceCounter(end_count);

// calculate time elapsed
long milliseconds_elapsed = (long)(((double)(end_count - start_count) / freq) * 1000);

// from here on you can format milliseconds_elapsed any way you need to

An alternative to the above example would be to use the TimeSpan structure available in .Net which has a constructor that takes ticks like so:

// then obtain your first reading
long start_count;
long end_count;
QueryPerformanceCounter(start_count)

// .. do some work

// obatin your second reading
QueryPerformanceCounter(end_count);

TimeSpan time_elapsed = new TimeSpan(end_count - start_count);

Console.WriteLine("Time Elapsed: " + time_elapsed.ToString());
Miky Dinescu
+4  A: 

The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it.

Hans Passant
A: 

Can use : 1) The System.Diagnostics.Stopwatch class uses QueryPerformanceCounter(), saves you from having to P/Invoke it. 2) Can use directly by importing from the Win32 dll . [DLLImport(Win32)] and the name ofthe function

Roman Dorevich