Ho riletto e rivisto il tutorial ma c'è qualcosa che ancora mi sfugge.
- La sintassi del comando "Wait For" mi pare che (adesso) sia solo "WaitFor ([sub]) Complete ([variabile restituita dalla sub] as [tipo di variabile restituita dalla sub]) e che non ci sia altra sintassi. O si?
Errato. la chiave COMPLETE seve solo per sospendere (o meglio far salire un livello superiore) il flusso dell'esecuzione finche la SUB non sia completa, ammesso che quella SUB sia una RESUMABLESUB
Ma il m,etodo standard è WAIT FOR SUBCHIAMATA(Parametri). Con questo metodo rimane in attesa finche venga chiama la SUBCHIAMATA da una CallSub(Modulo,"SUBCHIAMATA") oppure CallSub2(Modulo,"SUBCHIAMATA", Parametro1) ecc...
Sub DownloadImage(Link As String, iv As ImageView)
Dim job As HttpJob
job.Initialize("", Me) 'note that the name parameter is no longer needed.
job.Download(Link)
Wait For JobDone(job As HttpJob)
If job.Success Then
iv.SetImage (job.GetBitmap) 'replace with iv.Bitmap = job.GetBitmap in B4A / B4i
End If
job.Release
End Sub
- Una Resumable Sub pare debba restituire per forza una qualche variabile, un qualche risultato. Ma se a me non serve altro che l'esecuzione delle istruzioni ivi contenute prima che l'app vada avanti, se non ho capito male, devo farle restituire Null e nella sintassi di "WaitFor" il tipo di variabile restituita deve essere "Object". O c'è un altro modo? Escluderei Sleep, dato che se imposto 3 millisecondi e la Sub che ho richiamato poco prima ci impiega 7000 millisecondi, non accade quello che vorrei.
No, non sono obbligatori i parametri. Nel caso che usi COMPLETE, e solo in questo caso, devi usare una variabile.
WAIT FOR KeyboardFunction_KEYUP
- Una Resumable Sub può restituire più di un valore? Penso al caso in cui si vuol accedere ad un database e prendere due-tre valori. Le alternative che mi vengono in mente e che uso sono: (i) doppio codice; (ii) doppia chiamata; (iii) far restituire una list.
SI, ma usando CallSub3 al massimo puoi inserire 2 parametri. Meglio usare una Map per passare piu parametri, La useresti come fosse una Tupla
Mi perdonerete se una o più domande vi sembrano stupide - questa non è una domanda ma un'affermazione!
Qualche link da studiare
Resumable subs can only work when there is a message queue. By default, server handlers end when the Handle sub is completed. They do not create a message loop. If you want to wait for an event then you need to call StartMessageLoop and later StopMessageLoop. Example of handler that downloads...
Remember that that a call to Sleep or Wait For in a resumable sub causes the code flow to return to the parent. Example: Sub Button1_Click Sum(1, 2) Log("after sum") End Sub Sub Sum(a As Int, b As Int) Sleep(100) 'this will cause the code flow to return to the parent Log(a + b) End Sub...
New video tutorial: Resumable subs is a new feature added in B4J v5.50 / B4i v4.00 / B4A v7.00. It dramatically simplifies the handling of asynchronous tasks. (This feature is a variant of stackless coroutines.) The special feature of resumable subs is that they can be paused, without...
The ability of resumable subs to return values (added in B4A v7.3) makes it very simple to add voice recognition to your app. Code: Private Sub RecognizeVoice As ResumableSub vr.Listen Wait For vr_Result (Success As Boolean, Texts As List) If Success And Texts.Size > 0 Then...