Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Async Task ChainDemonstrates how to combine a sequence of asynchronous tasks into a single task chain to be run in a background thread.
IncludeFile "CkTask.pb" IncludeFile "CkGlobal.pb" IncludeFile "CkEmail.pb" IncludeFile "CkTaskChain.pb" IncludeFile "CkMailMan.pb" Procedure ChilkatExample() ; All Chilkat classes can be unlocked at once at the beginning of a program ; by calling UnlockBundle. It requires a Bundle unlock code. chilkatGlob.i = CkGlobal::ckCreate() If chilkatGlob.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkGlobal::ckUnlockBundle(chilkatGlob,"Anything for 30-day trial.") If success <> 1 Debug CkGlobal::ckLastErrorText(chilkatGlob) CkGlobal::ckDispose(chilkatGlob) ProcedureReturn EndIf 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.someMailServer.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) CkGlobal::ckDispose(chilkatGlob) 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." CkGlobal::ckDispose(chilkatGlob) 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) CkGlobal::ckDispose(chilkatGlob) 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) CkGlobal::ckDispose(chilkatGlob) 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) CkGlobal::ckDispose(chilkatGlob) 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) CkGlobal::ckDispose(chilkatGlob) 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 CkGlobal::ckDispose(chilkatGlob) CkMailMan::ckDispose(mailman) CkTaskChain::ckDispose(taskChain) CkEmail::ckDispose(email) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.