PureBasic
PureBasic
Thread Pool Size
See more Async Examples
Demonstrates how to set the maximum number of threads in Chilkat's thread pool manager. Also demonstrates how to set a thread pool log file for help in diagnosing unexpected problems.Chilkat PureBasic Downloads
IncludeFile "CkTask.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkGlobal.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
; Set the maximum number of threads in the Chilkat thread pool to 12.
; This means that no more than 12 background worker threads will exist simultaneously.
; If more than 12 tasks are queued then some must wait for a worker thread to become free.
; Note: The Chilkat thread pool manager thread is a thread distinct from the
; worker threads. It starts when the 1st asynchronous task is Run.
glob.i = CkGlobal::ckCreate()
If glob.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkGlobal::setCkMaxThreads(glob, 12)
; Also, the ThreadPoolLogPath can be set to cause the thread pool manager thread to
; keep a log file. This is for the purpose of debugging if unexpected problems occur.
CkGlobal::setCkThreadPoolLogPath(glob, "/home/users/chilkat/logs/threadPoolLog.txt")
http1.i = CkHttp::ckCreate()
If http1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
http2.i = CkHttp::ckCreate()
If http2.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
http3.i = CkHttp::ckCreate()
If http3.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
url1.s = "http://www.marcusmiller.com/"
url2.s = "http://www.tromboneshorty.com/"
url3.s = "http://www.jamesmorrison.com/"
; Call the async version of the QuickGetStr method to return a task object.
; The task object is loaded, but is in the Inert state -- meaning it is
; not yet scheduled to run on Chilkat's background thread pool.
task1.i = CkHttp::ckQuickGetStrAsync(http1,url1)
If CkHttp::ckLastMethodSuccess(http1) = 0
Debug CkHttp::ckLastErrorText(http1)
CkGlobal::ckDispose(glob)
CkHttp::ckDispose(http1)
CkHttp::ckDispose(http2)
CkHttp::ckDispose(http3)
ProcedureReturn
EndIf
task2.i = CkHttp::ckQuickGetStrAsync(http2,url2)
If CkHttp::ckLastMethodSuccess(http2) = 0
Debug CkHttp::ckLastErrorText(http2)
CkTask::ckDispose(task1)
CkGlobal::ckDispose(glob)
CkHttp::ckDispose(http1)
CkHttp::ckDispose(http2)
CkHttp::ckDispose(http3)
ProcedureReturn
EndIf
task3.i = CkHttp::ckQuickGetStrAsync(http3,url3)
If CkHttp::ckLastMethodSuccess(http3) = 0
Debug CkHttp::ckLastErrorText(http3)
CkTask::ckDispose(task1)
CkTask::ckDispose(task2)
CkGlobal::ckDispose(glob)
CkHttp::ckDispose(http1)
CkHttp::ckDispose(http2)
CkHttp::ckDispose(http3)
ProcedureReturn
EndIf
; At this point we have 3 task objects, each loaded with a Chilkat method call.
; Note: At this point no background threads are running. The thread pool manager
; thread is not started -- it will start when the very first task is Run.
; Schedule each task for running on the thread pool. This changes each task's state
; from Inert to Live. The thread pool manager thread starts with the 1st task queued.
; If the Global.ThreadPoolLogPath property was set, then
; the log file would be created (or appended) at this point.
success = CkTask::ckRun(task1)
; Assuming success for brevity...
success = CkTask::ckRun(task2)
success = CkTask::ckRun(task3)
; The application is now free to do anything else
; while the HTML at the URL's are being downloaded in background threads.
; In this case, we'll just wait for all three tasks to finish.
; All three tasks are running simultaneously in separate background threads.
; We can wait for each in any order. If Wait is called and the task has already
; finished (or been canceled), then the Wait method returns immediately.
maxWaitMs.i = 20000
success = CkTask::ckWait(task1,maxWaitMs)
success = CkTask::ckWait(task2,maxWaitMs)
success = CkTask::ckWait(task3,maxWaitMs)
; Assuming success for brevity...
err.s = "Task failed or canceled"
html1.s = err
html2.s = err
html3.s = err
; Now get the HTML downloaded in each task:
If (CkTask::ckStatusInt(task1) = 7) AND (CkTask::ckTaskSuccess(task1) = 1)
html1 = CkTask::ckGetResultString(task1)
EndIf
If (CkTask::ckStatusInt(task2) = 7) AND (CkTask::ckTaskSuccess(task2) = 1)
html2 = CkTask::ckGetResultString(task2)
EndIf
If (CkTask::ckStatusInt(task3) = 7) AND (CkTask::ckTaskSuccess(task3) = 1)
html3 = CkTask::ckGetResultString(task3)
EndIf
Debug html1
Debug "----"
Debug html2
Debug "----"
Debug html3
Debug "----"
CkTask::ckDispose(task1)
CkTask::ckDispose(task2)
CkTask::ckDispose(task3)
CkGlobal::ckDispose(glob)
CkHttp::ckDispose(http1)
CkHttp::ckDispose(http2)
CkHttp::ckDispose(http3)
ProcedureReturn
EndProcedure