Sample code for 30+ languages & platforms
PureBasic

Get the LastErrorText for an Asynchronous Method Call

See more Async Examples

Demonstrates how to get the LastErrorText information for a Chilkat method called asynchronously.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkTask.pb"

Procedure ChilkatExample()

    success.i = 0

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

    socket.i = CkSocket::ckCreate()
    If socket.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; The Async call simply creates the task.  The "Connect" method has not yet been called.
    maxWaitMs.i = 5000
    ; Let's intentionally cause this connect to fail by giving it an invalid domain..
    task.i = CkSocket::ckConnectAsync(socket,"amazonbladflakjsdflksadjf.com",443,1,maxWaitMs)
    If CkSocket::ckLastMethodSuccess(socket) = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ; Start the background thread to run the task.
    success = CkTask::ckRun(task)
    If Not success
        Debug CkTask::ckLastErrorText(task)
        CkTask::ckDispose(task)

        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ; The application is now free to do anything else

    ; For this example, we'll simply sleep and periodically
    ; check to see if the Socket Connect if finished.
    While CkTask::ckFinished(task) <> 1
        ; Sleep 1 ms.
        CkTask::ckSleepMs(task,1)
    Wend

    ; If the task completed, it means the method (in this case the Connect method) was called and returned success or failure.
    If CkTask::ckStatusInt(task) <> 7
        Debug "Task did not complete."
        Debug "task status: " + CkTask::ckStatus(task)
        CkTask::ckDispose(task)

        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ; Get the success/failure of the Connect
    ; (This is the return value of the Connect method had it been called synchronously)
    success = CkTask::ckGetResultBool(task)
    If success = 0
        ; Get the LastErrorText for the Connect method call.
        ; Had we called Connect synchronously, we would've simply accessed the socket object's LastErrorText property.
        ; Instead, we get the task object's ResultErrorText.
        Debug CkTask::ckResultErrorText(task)
    Else
        Debug "Connect succeeded."
    EndIf

    CkTask::ckDispose(task)



    CkSocket::ckDispose(socket)


    ProcedureReturn
EndProcedure