B4J Question Resumable Sub Question

PatrikCavina

Active Member
Licensed User
Can cause some issues a "resumable sub" that can return value without wait for or sleep?

example:

B4X:
Sub MySub(Destination As String, UseFTP As Boolean) As ResumableSub
    Dim success As Boolean = False
    If UseFTP Then
        Dim ftp As FTP
        ftp.Initialize("FTP", "xxx.xxx.xxx.xxx", 21, "user", "password")
        wait for (ftp.UploadFile("dirSource", "fileSource", True, Destination&"/fileSource")) FTP_UploadCompleted (ServerPath As String, succ As Boolean)
        success = succ
    Else
        File.Copy("dirSource", "fileSource", Destination, "fileTarget")
        If File.Exists("dirSource", "fileSource") Then success = True
    End If
    Return success
End Sub
If i don't use ftp the operation is made without async event.
Should i put a sleep(0) before of file.copy or can i do it without async function?

(I know the existence of file.copyAsync, this is only an example)
 

DonManfred

Expert
Licensed User
If i don't use ftp the operation is made without async event.
that´s not true. Without wait for you need to handle the FTP_UploadCompleted Event by yourself. The Upload itself is always Async.

Maybe you should declared it more specific what exactly you want to do.
 

OliverA

Expert
Licensed User
Alwas call MySub via
B4X:
Wait For (MySub(SomeDestinationString, SomeBooleanValue)) Complete (SomeVariableName as Boolean)
That part of the MySub sub (the file copying) does not is a WaitFor/Sleep does not matter, the sub always works as you want it (code after the WaitFor (MySub...) is executed only after MySub is done/returns a value). If you leave the Wait For out (Just call MySub), then unexpected results will happen when you are using FTP (code after the MySub is executed immediately when the Wait For in MySub is executed).

One caveat: As is, MySub is non-blocking for FTP and blocking for file copying. This may not be an issue for small files, but for larger files it may have an impact. I would just go ahead and use File.CopyAsync and this way MySub is behaving in a non-blocking manner for FTP and file copying.
 

PatrikCavina

Active Member
Licensed User
That part of the MySub sub (the file copying) does not is a WaitFor/Sleep does not matter, the sub always works as you want it (code after the WaitFor (MySub...) is executed only after MySub is done/returns a value). If you leave the Wait For out (Just call MySub), then unexpected results will happen when you are using FTP (code after the MySub is executed immediately when the Wait For in MySub is executed).
@OliverA I think you've hit the point
So regardless of what will be done between ftp.Upload File (Async) or File.Copy (Sync) the resumable sub will work correctly?
 
Top