ON TIMER equivalent using signals

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

ON TIMER equivalent using signals

Post by angros47 »

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: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: ON TIMER equivalent using signals

Post by VANYA »

I checked on systems and it work:

Linux mint 18 (64-bit)
DOS 6.22

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

In windows it will not work.
Post Reply