Link to home
Start Free TrialLog in
Avatar of sssssss
sssssss

asked on

Timer in 1/100 seconds

A timer to display the following time:
12:00:16.15
and refresh every 1/100 seconds.
I made one timer but there is a overhaed of 0.05 to 0.1
second.
The timer I made was using a timer object, a timer function
and a for loop procedure, but when in run, the unmber
diplay from the screen just not in sequence.
I assume that there could be a better function to solve this problem.
Avatar of schild
schild

this Help page will give you the needed answer:

timeSetEvent
The timeSetEvent function starts a specified timer event. The multimedia timer runs in its own thread. After the event is activated, it calls the specified callback function or sets or pulses the specified event object.

MMRESULT timeSetEvent(
  UINT uDelay,                
  UINT uResolution,          
  LPTIMECALLBACK lpTimeProc,  
  DWORD dwUser,              
  UINT fuEvent                
);
 
Parameters
uDelay
Event delay, in milliseconds. If this value is not in the range of the minimum and maximum event delays supported by the timer, the function returns an error.
uResolution
Resolution of the timer event, in milliseconds. The resolution increases with smaller values; a resolution of 0 indicates periodic events should occur with the greatest possible accuracy. To reduce system overhead, however, you should use the maximum value appropriate for your application.
lpTimeProc
Address of a callback function that is called once upon expiration of a single event or periodically upon expiration of periodic events. If fuEvent specifies the TIME_CALLBACK_EVENT_SET or TIME_CALLBACK_EVENT_PULSE flag, then the lpTimeProc parameter is interpreted as a handle to an event object. The event will be set or pulsed upon completion of a single event or periodically upon completion of periodic events.
dwUser
User-supplied callback data.
fuEvent
Timer event type. This parameter may include one of the following values. Value Meaning
TIME_ONESHOT Event occurs once, after uDelay milliseconds.
TIME_PERIODIC Event occurs every uDelay milliseconds.


The fuEvent parameter may also include one of the following values: Value Meaning
TIME_CALLBACK_FUNCTION When the timer expires, Windows calls the function pointed to by the lpTimeProc parameter. This is the default.  
TIME_CALLBACK_EVENT_SET When the timer expires, Windows calls the SetEvent function to set the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.  
TIME_CALLBACK_EVENT_PULSE When the timer expires, Windows calls the PulseEvent function to pulse the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.  



Return Values
Returns an identifier for the timer event if successful or an error otherwise. This function returns NULL if it fails and the timer event was not created. (This identifier is also passed to the callback function.)

Remarks
Each call to timeSetEvent for periodic timer events requires a corresponding call to the timeKillEvent function.


Good Luck
Schild
Avatar of sssssss

ASKER

Please give an example, the help page is not clear and is not help much
Avatar of sssssss

ASKER

I appreciate an answer with an example.
Greetings, sssssss,

If you're using the normal timer event, you might want to add the statement "DoEvents" inside every loop and IF-THEN-statement in the code for the timer. It allows the system to perform its normal functions while performing the timer almost at the same time.
Since the typical dos/windows-based pc does not allow multi tasking but only time sharing, you have to tell the system which functions are more serious than others. If you don't, the system will use its time for some other function and cause other functions to flicker. Taking up the "DoEvents" statement will allow the timer events to procede as they are planned: every 1/100 of a second.
Note: be careful with DoEvent! You might come to overuse it, and cause all kinds of unwanted things to happen. For instance: allowing the user to hit any button while a critical function hasn't completed, ensuring that the critical function will erronously complete after the hit instead of correctly before.

Imagine!
Avatar of sssssss

ASKER

using the below statement from help of the vb :

Start = Timer
    Do While Timer < Start + PauseTime
       LoopCount = LoopCount + 1
    Loop
    Finish = Timer
    TotalTime = Finish - Start
    Print TotalTime
    Print LoopCount

result will be the follwing :
PauseTime    TotalTime           Error            LoopCount
    1         1.039 - 1.049     0.039 - 0.049     4401 - 4580
  0.1        0.1099 - 0.1100    0.0099 - 0.01      245 - 452
  0.01       0.0049 - 0.06      0.0051 - 0.05       37 - 231

using this statement, still has 0.05 secound overhead, the smallest pause time
the bigger overhead. And the overhead will become bigger If the result disply from the
label in every loop.
Could this problem be solved?
Try to use API: GetTickCount


It is the best an lowestlevel timer the Windows may offer.

This time scale may recuire a special tuned computer.

For example chache memory speeds up computer, but not all the time, some time there will be mishit and you see it as jump in this timer. Also it is possible the Windows self may want to do
someting during this timing, it may scan network, update display and so on.  
The 0.05 secound overhead will be very difficult to
remove there if you run it on windows.
There is always bossible that some function call hangs on a litle.

To have better precision you may need to use an electronic
measuring instrument in PC slot, these have own sampling systems
independet what is gooing whit Windows.

It is currently regular PC, not a laboratory equipment.

I hope this helps you!

Matti

ASKER CERTIFIED SOLUTION
Avatar of anzen
anzen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sssssss

ASKER

Thanks!
I got what I want.