Source: CATIA Little Ant

When working on CATIA secondary development, there are times when you need to pause the program execution and let CATIA idle for a moment.
Here are four effective methods to implement delays in CATIA VBA:
1. Basic Delay Using Timer Function (Timing unit: seconds; 1 = 1 second)
If you want to limit the execution speed of an application interface and add a delay within a loop, the Timer function is a simple and convenient choice. While it’s not extremely precise, it’s suitable for delays of a few seconds. Timer is a built-in VBA function and is widely recommended in VB manuals. The general structure is as follows:
Sub delay(T As Single)
Dim time1 As Single
time1 = Timer
Do While Timer - time1 < T
DoEvents 'Allows the OS to process other events
Loop
Debug.Print "Run completed, total time taken: " & Timer - time1 & " seconds"
End Sub
You can call it like this:
Sub calculate1_time()
delay (1.5)
End Sub
2. Precise Delay Using Sleep Function (Timing unit: milliseconds; 1000 = 1 second)
For more precise delays, you can use the Sleep function, a Windows API call that pauses the current thread for a specified number of milliseconds. You must declare it before use:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
The Sleep function suspends the current thread without consuming CPU resources, unlike DoEvents which yields control to other processes but still keeps the CPU fully busy. For example, Sleep 1000 delays execution by 1 second.
Here is a sample implementation:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub calculate2_time()
Dim d As Date
d = Time()
Sleep 3000 'Pause for 3 seconds
Debug.Print "End of run, total time: " & DateDiff("s", d, Time()) & " seconds"
End Sub
3. Accurate Delay Using timeGetTime Function (Timing unit: milliseconds; 1000 = 1 second)
A more reliable method involves the timeGetTime Windows API function, which returns the number of milliseconds since system start. It supports millisecond-level precision and won’t roll over for about 49.7 days.
You must declare it before use:
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
This delay function works similarly to the first method but uses timeGetTime instead of Timer. The DoEvents statement ensures the system remains responsive:
Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
Sub delay(T As Long)
Dim time1 As Long
time1 = timeGetTime
Do
DoEvents 'Allows OS to handle other events
Loop While timeGetTime - time1 < T
End Sub
Calling example:
Sub ce_time()
Dim d As Date
d = Time()
Call delay(1000) 'Delay for 1 second
Debug.Print "Run completed, total time taken: " & DateDiff("s", d, Time()) & " seconds"
End Sub
4. Smart Use of VBA’s Built-in DateAdd Function (Timing unit: seconds; 1 = 1 second)
You can also create a delay by comparing the current time with a target end time using the DateAdd function:
Public Sub BKWait(HowManySecs As Integer)
'Pause execution for HowManySecs seconds
Dim EndWait As Date
EndWait = DateAdd("s", HowManySecs, Now)
While Now < EndWait
'Do nothing during the wait
Wend
End Sub
Example usage:
Sub calculate4()
BKWait(5) 'Pause for 5 seconds
End Sub

















Must log in before commenting!
Sign Up