Sample code for 30+ languages & platforms
Xbase++

Async Task Chain (another example)

See more Async Examples

Demonstrates using a task chain to run a sequence of FTP tasks asynchronously.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oFtp
LOCAL oTaskChain
LOCAL oTask
LOCAL cLocalFilename
LOCAL cRemoteFilename
LOCAL nNumTasks
LOCAL nTaskIdx

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

nSuccess := 0

oFtp := CreateObject("Chilkat.Ftp2")

oFtp:Hostname := "ftp.example.com"
oFtp:Username := "login"
oFtp:Password := "password"

//  Connect and login to the FTP server.
nSuccess := oFtp:Connect()
IF (nSuccess != 1)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

oTaskChain := CreateObject("Chilkat.TaskChain")

//  Create a task to change to the remote directory where the file will be uploaded.
oTask := oFtp:ChangeRemoteDirAsync("junk")
IF (oFtp:LastMethodSuccess == 0)
    ? oFtp:LastErrorText
    oFtp:destroy()
    oTaskChain:destroy()
    RETURN
ENDIF

//  Add this task to the task chain.
nSuccess := oTaskChain:Append(oTask)
oTask:destroy()

//  Create a task to upload a file.
cLocalFilename := "c:/temp/hamlet.xml"
cRemoteFilename := "hamlet.xml"

oTask := oFtp:PutFileAsync(cLocalFilename, cRemoteFilename)
IF (oFtp:LastMethodSuccess == 0)
    ? oFtp:LastErrorText
    oFtp:destroy()
    oTaskChain:destroy()
    RETURN
ENDIF

//  Add this task to the task chain.
nSuccess := oTaskChain:Append(oTask)
oTask:destroy()

//  Start the task chain running in a background thread.
//  Each task is run one after the other (on the same background thread) until all tasks have completed.
//  The task chain will stop at the first task that fails.
oTaskChain:StopOnFailedTask := 1
nSuccess := oTaskChain:Run()
IF (.NOT. nSuccess)
    ? oTaskChain:LastErrorText
    oFtp:destroy()
    oTaskChain:destroy()
    RETURN
ENDIF

//  The application is now free to do anything else
//  while the FTP commands are being run...

//  For this example, we'll simply sleep and periodically
//  check to see if the taskchain if finished. 
DO WHILE oTaskChain:Finished != 1

    //  Sleep 100 ms.
    oTaskChain:SleepMs(100)

ENDDO

//  A finished task chain could be one that was canceled, aborted, or truly finished.  

//  If the task chain "completed", then it ran to completion.  A "completed" task will
//  have a StatusInt equal to 7.   If the task finished, but was not completed, then it must've
//  been aborted or canceled:
IF (oTaskChain:StatusInt != 7)
    ? "Task did not complete."
    ? "task chain status: " + oTaskChain:Status
    oFtp:destroy()
    oTaskChain:destroy()
    RETURN
ENDIF

//  If we got to this point, the ChangeRemoteDir and PutFile were successful.
//  We can visually verify by examining the LastErrorText that was recorded for each
//  of these method calls..
nNumTasks := oTaskChain:NumTasks
nTaskIdx := 0

DO WHILE (nTaskIdx < nNumTasks)

    oTask := oTaskChain:GetTask(nTaskIdx)

    //  Examine the status of this task, and the ResultErrorText
    //  (the ResultErrorText is the ftp.LastErrorText captured for FTP method called by the task).
    //  Everything should indicate success.
    ? "task status: " + oTask:Status
    ? "task log: " + oTask:ResultErrorText

    oTask:destroy()

    nTaskIdx := nTaskIdx + 1
ENDDO

nSuccess := oFtp:Disconnect()

oFtp:destroy()
oTaskChain:destroy()