Showing results for 
Search instead for 
Did you mean: 

Delay, Timer, and Sleep values are wildly off

Delay, Timer, and Sleep values are wildly off

The results of the Delay, Timer, and Sleep functions in Lab Windows 2010 seem to vary greatly. This experiment was done on a Windows XP SP3 32-bit system and a WIndows 7 64-bit system using this code right at the start of main():


    double time1;
    double time2;
    char s[100];

    time1 = Timer ();
    sprintf (s, "Time = %f, 0 sec", time1);
    MessagePopup ("first", s);
    time1 = Timer ();
    time2 = Timer ();
    sprintf (s, "Time = %f (%f - %f), 5 sec with Delay()",
        time2 - time1, time2, time1);
    MessagePopup ("second", s);
    time1 = Timer ();
    time2 = Timer ();
    sprintf (s, "Time = %f (%f - %f), another 5 sec with Sleep()",
        time2 - time1, time2, time1);
    MessagePopup ("third", s);


Reading the popups and measuring elapsed time with a stopwatch (between clicking OK and display of the next popup):


Windows XP SP3 32-bit:

useDefaultTimer registry setting = False (the default setting)

Delay: measured 24 sec, Timer delta: 5.001004

Sleep: measured  5 sec, Timer delta: 1.040915

useDefaultTimer = True

Delay: measured  3 sec, Timer delta: 4293204.328125 (this varies)

Sleep: measured  5 sec, Timer delta: 705.031250 (this is fairly consistent)


Windows 7 64-bit:

useDefaultTimer registry setting = False (the default setting)

Delay: measured  5 sec, Timer delta: 5.001004

Sleep: measured  5 sec, Timer delta: 4.994530

useDefaultTimer = True

Delay: measured  3 sec, Timer delta: 2532.000000 (this varies wildly)

Sleep: measured  5 sec, Timer delta: 705.032704 (this is fairly consistent)


What could be happening here?




0 Kudos
Message 1 of 13

Re: Delay, Timer, and Sleep values are wildly off


Delay() NI function has had curious behavior noted before, there are a couple of threads on this forum about it.


NI told me that Delay() incorporates a "ProcessSystemEvents" at least some of the time (?) and others have claimed it simply spinlocks.


Some of us wind up creating a delay function using the WinAPI Sleep() or SleepEx() function as it's more reliable (as your data shows) since the OS is simply putting the thread to sleep and there's nothing else that can come into play, though the Windows OS has the final say as to when your thread gets rescheduled - you are vulnerable to cheduling activity decisions made by the OS.  


I almost never use Delay().


Having said that, you shouldn't be seeing these types of errors on Delay() unless you've got something going on that a ProcessSystemEvents call (which is maybe embedded in the NI Delay() function) gets hung up and spends a lot of time processing. 

Do the times you measure correspond to how long you wait before clicking the OK popup button?

0 Kudos
Message 2 of 13

Re: Delay, Timer, and Sleep values are wildly off

Yes, the "measured" times are the approximate time in seconds (as manually measured with a stopwatch) from one "OK" click until the next dialog box appears. This test code is before the main panel is loaded or displayed so hopefully there isn't too much else going on to skew the results.


Using Sleep() sounds like a good idea anyway. Timer() doesn't seem all too useful on the XP machine. It is very curious that on XP with useDefaultTimer = false that Delay() appears to conform to Timer() in that they're both off by about a factor of 5.



0 Kudos
Message 3 of 13

Re: Delay, Timer, and Sleep values are wildly off

Hi Brian,


I ran your code in Interactive Execution window on CVI 6.0 (only version I have on this computer) and the results don't vary at all, let alone by those wild figures.

Delay results in 5.001 sec and Sleep results in 4.988 seconds as the Timer() difference measures.

These are also consistent with my iPod's stopwatch.


Did you loop them some large number of times and picked the wildest results or do they act so weird each time you call them?


Did I understand your experiment setup wrong or were you missing something?

0 Kudos
Message 4 of 13

Re: Delay, Timer, and Sleep values are wildly off

No, the same behavior occurs every single time. I didn't do anything special in the experiment; the program just has that code sitting in "main()" and that's all.


Since yesterday I asked someone else to run the same code on a different but similar computer to my XP SP3 32-bit machine. The other computer works correctly and doesn't show the same results that I see.


Another thing that may or may not be significant is that I had LabWindows/CVI 8.5 installed before installing 2010, so both of these are installed on my machine. I tried building and running the same code under 8.5 and got the same bad results for the various cases (Delay and Sleep and useDefaultTimer True and False). But the other computer that worked correctly is also in the same state; 8.5 was installed and then 2010 was installed after.


I have not yet tried to uninstall anything, but I was thinking about uninstalling everything and reinstalling only version 2010, especially since now the problem appears isolated to the one computer.

0 Kudos
Message 5 of 13

Re: Delay, Timer, and Sleep values are wildly off



Reinstalling LabWindows didn't help, but I tried the "counter.cws" sample program and still got poor results. This uses QueryPerformanceCounter() and I saw that its values were off right along with Delay(). This pointed to a problem with this specific computer, not with LabWindows. Some research showed someone else having a similar result using QueryPerformanceCounter() on a computer similar to mine, but not on other computers they tried.


A BIOS upgrade did the trick. Now for the default setting (useDefaultTimer = False) everything works great!


The other setting (useDefaultTimer = True) still works poorly; on all machines I've tried, a 5-second Delay() is only 3 seconds, and the Timer() values don't make any sense. But I have no reason not to use the default setting, so all is well.

0 Kudos
Message 6 of 13

Re: Delay, Timer, and Sleep values are wildly off

Sleep? what library is this in?


0 Kudos
Message 7 of 13

Re: Delay, Timer, and Sleep values are wildly off

Sleep() and SleepEx() are part of the Windows API, sometimes called the WinAPI, and if you're on a 32 bit Windows OS it's called the Win32API.


You include windows.h as the first include (so that the NI header files can accommodate the windows.h definitions)


#include <windows.h>


These functions are described in the Win32 SDK included with the full version of CVI, but they are also described on the MSDN website.  Just google SleepEx and you'll get a link to the Microsoft documentation for these.


Remember that a Sleep() call may not return for a longer period than what you ask for:  if a higher priority thread is running or available for scheduling, your thread won't run right away after finishing the Sleep().  But, the NI Delay() function has the same issue - if there's a higher priority thread than the thread Delay() is running on, it will run instead of your thread, potentially making the delay longer than you want.


I guess I'd look at Delay(), Sleep(), and SleepEx() as being functions that guarantee that your thread will sleep at least as long as you ask for.  Sleep() and SleepEx() are simpler than Delay() in that they just ask the OS to suspend the calling thread and then wake it up - there's nothing else happening behind the scenes.


NI provides the SyncWait() function to try and overcome this - this function tries to accommodate variable execution times and still yield a consistent delay or more correctly a consistent elapsed time.

Message 8 of 13

Re: Delay, Timer, and Sleep values are wildly off

There is no fp for the Sleep() or the SleepEx() at least when I #include "windows.h" still does not show a function callback on the open/close printhisis... so what's up?




0 Kudos
Message 9 of 13

Re: Delay, Timer, and Sleep values are wildly off

Function panels are not present for Win API functions: simply copy the definition from the interface to the API shiped with CVI or from MSDN and use it.

Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
If I have helped you, why not giving me a kudos?
0 Kudos
Message 10 of 13