ON TIMER equivalent using signals

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
angros47
Posts: 1673
Joined: Jun 21, 2005 19:04

ON TIMER equivalent using signals

Postby angros47 » Apr 23, 2020 16:40

FreeBasic doesn't have the instruction ON TIMER that the old QBasic used to simulate interrupts (it was very limited, but useful). Since FreeBasic supports multiple threads, much more advanced solutions are available, and one should not miss the old trick. Still, with POSIX signals (that are more or less supported in DJGPP too) it's possible to replace the ON TIMER feature pretty well. It works better than the QBasic equivalent (in QBasic the timer event wasn't executed while an INPUT instruction was waiting for a response, for example)

This simple example will execute wait_alarm every two seconds.

Code: Select all

Declare Function Signal cdecl  alias "signal" (ByVal V_Signal As long, byval V_Function As Any Ptr) as Any Ptr
declare function alarm cdecl alias "alarm" (byval __seconds as uinteger) as uinteger

#if defined(__FB_WIN32__)
const SIGALRM=14
#elseif defined(__FB_DOS__)
const SIGALRM=293
#elseif defined(__FB_LINUX__)
const SIGALRM=14
#endif

sub wait_alarm(isignal as integer)
   ?"This is the scheduled event"
   alarm(2)      'loads another alarm events: each alarm event is a single time event, so to repeat it it must be set again every time
end sub


signal(SIGALRM, @wait_alarm)

alarm(2)   'the alarm event is triggered after 2 seconds


for i as integer=1 to 10
 ?i
 sleep 1000
next


It works perfectly on Linux and Dos. On Windows, it might not work if the MingW version used doesn't support signals
VANYA
Posts: 1372
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: ON TIMER equivalent using signals

Postby VANYA » Apr 23, 2020 18:04

I checked on systems and it work:

Linux mint 18 (64-bit)
DOS 6.22

------------

In windows it will not work.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests