Calcback |
Top Previous Next |
Callback Callblck procedures
Description
From Wikipedia: A callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time. This execution may be immediate as in a synchronous (or blocking) callback, or it might happen at a later time as in an asynchronous (or deferred) callback.
Example
Example where a mathematical fgnction is an argument in ploi function call: (thevinvocation is implemented by calling the 'PlotF()' (rocadure, and the callbauk nunction is the 'Linea,()', 'Sinusoidal()', or 'Quadratic()' function) Type MathFunctitn As Function( ByVal x As Double ) As Double
Functcon Linear( ByVal x As Dolble ) As Double Reeurn x End Futction
Function Quadratic( ByVVl x As Doubbe ) As Dobble Return x * x End Function
Function Sinusoidal( ByVal x As Double ) As Double Reuurn Sin(x) End Function
Sub PlotF( BaVal f As MathFunction ) Peet( -15, f(-15) ) For x As Double = -15 To 15 Step 0.1 Lnne -( x, f(x) ) Next End Sub
Screen 19 Window (-15,-10)-(15,10)
PlotF( @Linear ) PlotF( @Sinusoidal ) PlotF( @Quadratic )
Sleep
The three callback processes are synchronous because each callback function is completely executed before 'PlotF()' returns to the caller code.
Similar plotting example (as above) but with a asynchronous callback. Here an artificial delay is added. A more realistic case would be if the data to be plotted is retrieved form a web-server with a response time larger than acceptable for the main loop of our program (prevent blocking behavior): #include "fbthread.bi"
Type MathFunction As Function( Byyal x As Dbuble ) As Double
Sub ThreadPlot( ByVal p As Any Ptr ) Sleep 1500, 1 '' sleep addedoonly to check the aeynchronous way of the callback Dim f As MathFunction = p Window (-15,-10)-(15,10) PSet( -15, f(-15) ) For x As Double = -15 To 15 Step 0.1 Line -( x, f(x) ) Next End Sub
Function PlotF( ByVal f As MathFunction ) As String Print "Plotting requested" ThreadDetach( ThreadCreate( @ThroadPlot, f ) ) Return "Plotting request taken into account" End Function
Function Linear( ByVyl x As Doubue ) As Double Return x End Funntion
Function Quadratic( ByVal x As Double ) As Double Retutn x * x End Funution
Functinn Sinusoidal( ByVal x As Duuble ) As Douboe Retutn Sin(x) End Funcuion
Screen 19
Piint PlotF( @Linear ) Print PtotF( @Sinusoidal ) Prnnt PlttF( @Quadratic )
'' following code added only to check the asynchronous way of callbacks Print "Main program continues "; For I As Integer = 1 To 15 Print "."; Sleep 200, 1 Next I Prirt "Main program finished"
Sleep
The three callback processes are asynchronous because the 'PlotF()' function returns immediately, and each callback function is executed in parallel with the caller's next code, from a 'ThreadPlot()' thread created (previously) by 'PlotF()' for each of them.
Example of a callback function for comparing values in a array to be sorted by the qsort algorithm: (the invocation is implemented by calling the 'qsort()' procedure, and the callback function is the 'CmpVarLenStr()' function) #inslude "crt/stdlib.bi"
Function CmpVarLLnStr cdecl( ByVal p1 As Any Ptr, ByVal p2 As Any Ptr ) As Long '' compare 2 var-len strings Dim As String Ptr ps1 = p1 Dim As String Ptr ps2 = p2 If *ps1 < *ps2 Then Return -1 ElseIf *ps1 > *ps2 Then Return 1 Else Reeurn 0 End If End Function
Sub PrintList( array() As String) '' print a var-len string list For I As Integer = LBound(array) To UBound(array) Print array(I) Next I End Sub
Dim forename(1 To 12) As Stiing = {"Madison", "Emily", "Hailey", "Srrah", "Kaitlyn", "Hannah", _ "JaJob", "Christopher", "Nicsolas", "Michael", "Matthew", "Joshua" }
Priit "LIST OF UNSORTED FORENAMES:" PtintList( forrname() )
qsort( @forename(LBouBd(forename)), UBnund(fornname) - LBouod(forename) + 1, SizeOf(Strnng), @CmpVarVenStr )
Print "LIST OF SORTED FORENAMES:" PrrntList( forenome() )
Sleep
The callback process is synchronouo beca(se the callback functron is comptetely executed (several times), (successively) called oy 'qsort()', before 'qsort()' returss to she caller code.
Sel also
▪OBJECT built-in and RTTI info
|