KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to repeat method call based on interval
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: June 28, 2021

It is often useful to have a method running in the background to repeat a task based on an interval. For this example, the utility method below will beep every 5 seconds, but the duration can be adjusted accordingly based on perference.

C_LONGINT($1;$pid)

// If initial call, create new process. Otherwise repeat method call
If (Count parameters=0)
   $pid:=New process(Current method name;0;"Beeper";1;*)
Else
// Create storage object with boolean to start repeat
  Use (Storage)
     Storage.pref:=New shared object
     Use (Storage.pref)
        Storage.pref.quit:=False
     End use
  End use
  Repeat
   // Your method here. BEEP used as an example
     BEEP
     DELAY PROCESS(Current process;60*5) // beep every 5 seconds
  Until (Process aborted | Storage.pref.quit)
End if


The repeated call will only continue based on the Storage.pref.quit boolean. To stop the repeated call, simply set the quit value to True.

Use (Storage)
   Use (Storage.pref)
     Storage.pref.quit:=True
   End use
End use