LabWindows™/CVI™ Code Runs Faster When NI MAX Is Opened

Updated May 30, 2024

Reported In

Software

  • LabWindows/CVI
  • Measurement & Automation Explorer (MAX)

Issue Details

I have noticed that when I run my LabWindows™/CVI™ code with NI MAX already opened in the background, my application runs faster than it usually does. How can I get my code to execute this fast without having to open NI MAX?  

Solution

If you are using NI instruments, such as digital multimeters, function generators, and oscilloscopes, the fetching process will attempt to sleep for 1 millisecond at a time if data is not available. If the default system clock resolution on the test system is 10 milliseconds this issue can be seen, as a 1 millisecond sleep may take 10 milliseconds instead. I/O Trace and NI MAX configure the system clock resolution to 1 millisecond, which can cause the difference in the behavior.

As a workaround, configure the system clock resolution in the LabWindows™/CVI™ application by adding the following code:

#include "windows.h"
#include "mmsystem.h"
#include "stdint.h"

...
const uint32_t desiredTimerResolutionInMilliseconds = 1;
TIMECAPS timeCaps = {0,0};
timeGetDevCaps(&timeCaps, sizeof(timeCaps));
uint32_t timerResolutionInMilliseconds = min(max(timeCaps.wPeriodMin, desiredTimerResolutionInMilliseconds), timeCaps.wPeriodMax);
timeBeginPeriod(timerResolutionInMilliseconds);

//Insert Application Logic Here//

timeEndPeriod(timerResolutionInMilliseconds);
...

Additional Information

The resolution of sleep depends on the resolution of the system clock, which is globally configured. The selected system clock resolution can be observed using the Windows Sysinternals ClockRes tool.