PureBasic
PureBasic
Async Task Chain
See more Async Examples
Demonstrates how to combine a sequence of asynchronous tasks into a single task chain to be run in a background thread.Chilkat PureBasic Downloads
IncludeFile "CkTask.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkTaskChain.pb"
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
success = 0
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Set the POP3 server's hostname
CkMailMan::setCkMailHost(mailman, "pop.example.com")
; Set the POP3 login/password and any other requirements..
CkMailMan::setCkPopUsername(mailman, "myLogin")
CkMailMan::setCkPopPassword(mailman, "myPassword")
CkMailMan::setCkPopSsl(mailman, 1)
CkMailMan::setCkMailPort(mailman, 995)
; Connect to the POP3 server:
success = CkMailMan::ckPop3BeginSession(mailman)
If Not success
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
; Get the number of messages in the mailbox.
numMessages.i = CkMailMan::ckGetMailboxCount(mailman)
If numMessages = 0
Debug "No email messages in the POP3 mailbox."
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
taskChain.i = CkTaskChain::ckCreate()
If taskChain.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Create async task objects to fetch each email message
; by its sequence number
i.i
task.i
For i = 1 To numMessages
task = CkMailMan::ckFetchByMsgnumAsync(mailman,i)
If CkMailMan::ckLastMethodSuccess(mailman) = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
CkTaskChain::ckDispose(taskChain)
ProcedureReturn
EndIf
; Append each task to the task chain.
success = CkTaskChain::ckAppend(taskChain,task)
If Not success
Debug CkTaskChain::ckLastErrorText(taskChain)
CkMailMan::ckDispose(mailman)
CkTaskChain::ckDispose(taskChain)
ProcedureReturn
EndIf
CkTask::ckDispose(task)
Next
; At this point, no tasks have actually started running.
; All we've done so far is to create tasks for the work that will be done
; when the task chain is run.
; 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.
CkTaskChain::setCkStopOnFailedTask(taskChain, 1)
success = CkTaskChain::ckRun(taskChain)
If Not success
Debug CkTaskChain::ckLastErrorText(taskChain)
CkMailMan::ckDispose(mailman)
CkTaskChain::ckDispose(taskChain)
ProcedureReturn
EndIf
; The application is now free to do anything else
; while the emails are being downloaded
; For this example, we'll simply sleep and periodically
; check to see if the taskchain if finished.
While CkTaskChain::ckFinished(taskChain) <> 1
; Sleep 100 ms.
CkTaskChain::ckSleepMs(taskChain,100)
Wend
; 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 CkTaskChain::ckStatusInt(taskChain) <> 7
Debug "Task did not complete."
Debug "task chain status: " + CkTaskChain::ckStatus(taskChain)
CkMailMan::ckDispose(mailman)
CkTaskChain::ckDispose(taskChain)
ProcedureReturn
EndIf
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
numTasks.i = CkTaskChain::ckNumTasks(taskChain)
taskIdx.i = 0
While (taskIdx < numTasks)
task = CkTaskChain::ckGetTask(taskChain,taskIdx)
; Load the email object with email downloaded by this task.
success = CkEmail::ckLoadTaskResult(email,task)
If Not success
Debug "Failed to load email object for task."
Else
Debug CkEmail::ckFrom(email) + "; " + CkEmail::ckSubject(email)
EndIf
CkTask::ckDispose(task)
taskIdx = taskIdx + 1
Wend
CkMailMan::ckDispose(mailman)
CkTaskChain::ckDispose(taskChain)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure