Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoGlob
    Handle hoHttp1
    Handle hoHttp2
    Handle hoHttp3
    String sUrl1
    String sUrl2
    String sUrl3
    Variant vTask1
    Handle hoTask1
    Variant vTask2
    Handle hoTask2
    Variant vTask3
    Handle hoTask3
    Integer iMaxWaitMs
    String sErr
    String sHtml1
    String sHtml2
    String sHtml3
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    Move False To iSuccess

    // 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. 
    Get Create (RefClass(cComChilkatGlobal)) To hoGlob
    If (Not(IsComObjectCreated(hoGlob))) Begin
        Send CreateComObject of hoGlob
    End
    Set ComMaxThreads Of hoGlob To 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.
    Set ComThreadPoolLogPath Of hoGlob To "/home/users/chilkat/logs/threadPoolLog.txt"

    Get Create (RefClass(cComChilkatHttp)) To hoHttp1
    If (Not(IsComObjectCreated(hoHttp1))) Begin
        Send CreateComObject of hoHttp1
    End
    Get Create (RefClass(cComChilkatHttp)) To hoHttp2
    If (Not(IsComObjectCreated(hoHttp2))) Begin
        Send CreateComObject of hoHttp2
    End
    Get Create (RefClass(cComChilkatHttp)) To hoHttp3
    If (Not(IsComObjectCreated(hoHttp3))) Begin
        Send CreateComObject of hoHttp3
    End

    Move "http://www.marcusmiller.com/" To sUrl1
    Move "http://www.tromboneshorty.com/" To sUrl2
    Move "http://www.jamesmorrison.com/" To sUrl3

    // 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.
    Get ComQuickGetStrAsync Of hoHttp1 sUrl1 To vTask1
    If (IsComObject(vTask1)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTask1
        Set pvComObject Of hoTask1 To vTask1
    End
    Get ComLastMethodSuccess Of hoHttp1 To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp1 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComQuickGetStrAsync Of hoHttp2 sUrl2 To vTask2
    If (IsComObject(vTask2)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTask2
        Set pvComObject Of hoTask2 To vTask2
    End
    Get ComLastMethodSuccess Of hoHttp2 To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp2 To sTemp1
        Showln sTemp1
        Send Destroy of hoTask1
        Procedure_Return
    End

    Get ComQuickGetStrAsync Of hoHttp3 sUrl3 To vTask3
    If (IsComObject(vTask3)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTask3
        Set pvComObject Of hoTask3 To vTask3
    End
    Get ComLastMethodSuccess Of hoHttp3 To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp3 To sTemp1
        Showln sTemp1
        Send Destroy of hoTask1
        Send Destroy of hoTask2
        Procedure_Return
    End

    // 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.
    Get ComRun Of hoTask1 To iSuccess
    // Assuming success for brevity...
    Get ComRun Of hoTask2 To iSuccess
    Get ComRun Of hoTask3 To iSuccess

    // 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.
    Move 20000 To iMaxWaitMs
    Get ComWait Of hoTask1 iMaxWaitMs To iSuccess
    Get ComWait Of hoTask2 iMaxWaitMs To iSuccess
    Get ComWait Of hoTask3 iMaxWaitMs To iSuccess
    // Assuming success for brevity...

    Move "Task failed or canceled" To sErr
    Move sErr To sHtml1
    Move sErr To sHtml2
    Move sErr To sHtml3

    // Now get the HTML downloaded in each task:
    Get ComStatusInt Of hoTask1 To iTemp1
    Get ComTaskSuccess Of hoTask1 To bTemp1
    If ((iTemp1 = 7) And (bTemp1 = True)) Begin
        Get ComGetResultString Of hoTask1 To sHtml1
    End

    Get ComStatusInt Of hoTask2 To iTemp1
    Get ComTaskSuccess Of hoTask2 To bTemp1
    If ((iTemp1 = 7) And (bTemp1 = True)) Begin
        Get ComGetResultString Of hoTask2 To sHtml2
    End

    Get ComStatusInt Of hoTask3 To iTemp1
    Get ComTaskSuccess Of hoTask3 To bTemp1
    If ((iTemp1 = 7) And (bTemp1 = True)) Begin
        Get ComGetResultString Of hoTask3 To sHtml3
    End

    Showln sHtml1
    Showln "----"
    Showln sHtml2
    Showln "----"
    Showln sHtml3
    Showln "----"

    Send Destroy of hoTask1
    Send Destroy of hoTask2
    Send Destroy of hoTask3


End_Procedure