このコンテンツは設定された言語で表示できません。

このコンテンツは表示可能な別の言語で表示されています。 お使いのブラウザに翻訳機能がある場合はご利用ください。

The Difference Between the Wait (ms) Function and the Wait Until Next ms Multiple Function

Updated Aug 18, 2023

Reported In

Software

  • LabVIEW

Issue Details

I have heard of the Wait (ms) and the Wait Until Next ms Multiple functions, but it seems that they both accomplish the same thing. What is the difference between the Wait (ms) and Wait Until Next ms Multiple functions in LabVIEW?

Solution

The key concept you have to keep in mind is that while both functions use the operating system's millisecond timer as a reference for counting the time to wait, there is a difference in how long the functions will wait on their first iteration. 

Wait Until Next ms Multiple will wait until the value in the OS millisecond timer is a multiple of its input value at every iteration. Thus, Wait Until Next ms Multiple may not wait the specified time on the first iterations as it is adjusting the loop timing to be "in phase" (synched) with the OS timer. You can use this function to synchronize activities in different loops since they should execute in phase with the OS timer and each other.

The Wait (ms) function waits the specified time in ms regardless of the current time value in the OS timer. It doesn't care about getting in phase with the OS timer. Instead, it complies with the time to wait you defined in its input in every iteration.

The function icons also illustrate that difference (see the icons in the image below). Wait (ms) has a watch as its icon whereas Wait Until Next ms Multiple has a metronome, which is used by musicians to force them to adjust and keep the same pace.

The figure below demonstrates the timeline for execution of two loops. The black horizontal axis represents the OS millisecond timer count; the red line represents the timeline of loop 1, which uses Wait Until Next ms Multiple; the blue line represents the timeline of loop 2, which uses Wait (ms). The dots represent when the timer function will allow the loop to iterate.

Considering that both functions receive an input of 100, and that the OS timer has a value of 50 at the beginning of both loop executions:
  • Wait Until Next ms Multiple will wait until the OS timer is a multiple of 100 ms. Since this function was called when the OS timer was at 50 ms, Wait Until Next ms Multiple wait for only 50 ms before allowing the loop to iterate. Subsequent iterations will take 100ms and will occur when the OS timer values are multiples of 100 (i.e. 200, 300, 400, etc.). If occasionally the loop takes more than 100ms, on the very next iteration, Wait Until Next ms Multiple will force a shorter iteration period in order to get back in phase with the OS counter.
  • Wait (ms) will wait the specified 100 ms on every iteration, regardless of the OS timer value when the loop started execution. Notice that the iterations in the figure below are occurring at 150, 250, etc... but those values have no meaning to Wait (ms). If occasionally the loop takes more than 100ms, on the very next iteration, Wait (ms) will time the loop again with 100 ms, regardless of the OS timer value.
 


You can also observe the difference in execution using the following code examples.

Wait (ms)

The Wait (ms) function will block execution until the time specified at milliseconds to wait has elapsed.
  • Example 1: Consider a loop with code that takes 5 ms to execute. The loop also includes a Wait (ms) function with 10 ms wired to its milliseconds to wait input. The While Loop takes 10 ms total to execute because the code finishes after 5 ms and then the Wait (ms) function finishes 5 ms later. In this case, the Wait (ms) function executes in parallel with the code.

In this example, the loop timing is set by the outer Wait (ms) function.
  • Example 2: The loop is the same as in the previous example, but the code now takes 15 ms to execute. The outer Wait (ms) function still has 10 ms wired to its milliseconds to wait input. Because the time wired to the Wait (ms) function is less than the time it takes the code to execute, there is no delay after the code finishes, and the loop moves to the next iteration immediately after 15 ms elapses.
 

In this example, the loop timing is not set by the Wait (ms) function. It is set by the overall execution time of the code within the loop.

Wait Until Next ms Multiple

You can use the Wait Until Next ms Multiple function to synchronize separate loops to your system's millisecond clock. As the name suggests, it will wait until the next multiple of the number of milliseconds specified at the milliseconds multiple input before becoming unblocking.
  • Example 3: There is a loop with code that takes 100 ms to execute and a Wait Until Next ms Multiple function running in parallel with the code. The Wait Until Next ms Multiple function has 200 ms wired to its millisecond multiple input. The loop executes at every 200 ms multiple of the system's millisecond clock. There is second loop in the same VI that has a Wait Until Next ms Multiple function with 200 ms wired to its millisecond multiple input. The code in the second loop takes 150 ms to execute running in parallel. The two loops will be synchronized to move to their next iterations every time a multiple of 200 ms on the system clock elapses. Using this method, it will be insured that each loop begins subsequent iterations at the same time.
 

Additional Information

The milliseconds to wait and millisecond multiple values must be greater than the time the code within the loop takes to execute, otherwise, the wait time will never be respected. Besides the timing functionality, the wait functions are also responsible for blocking the code execution for a certain period of time, granting CPU to be used on other parts of the code. Not timing your loops correctly may generate an increase in CPU allocation, and in some cases, make your application hang.

Refer to the following LabVIEW Help articles for more information about the differences between the Wait (ms) function and the Wait Until Next ms Multiple function.