Sample code for 30+ languages & platforms
Xbase++

Async Methods Returning an Object

See more Async Examples

Demonstrates how to call an asynchronous method that returns an object. This example reads email from a POP3 server using the Async versions of the Chilkat methods.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oMailman
LOCAL oTask
LOCAL nMaxWaitMs
LOCAL nNumMessages
LOCAL oEmail
LOCAL i

nSuccess := 0

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

nSuccess := 0

oMailman := CreateObject("Chilkat.MailMan")

//  Set the POP3 server's hostname
oMailman:MailHost := "pop.example.com"

//  Set the POP3 login/password and any other requirements..
oMailman:PopUsername := "myLogin"
oMailman:PopPassword := "myPassword"
oMailman:PopSsl := 1
oMailman:MailPort := 995

//  Connect to the POP3 server:
oTask := oMailman:Pop3BeginSessionAsync()
IF (oMailman:LastMethodSuccess == 0)
    ? oMailman:LastErrorText
    oMailman:destroy()
    RETURN
ENDIF

//  Start the background task.
nSuccess := oTask:Run()
IF (.NOT. nSuccess)
    ? oTask:LastErrorText
    oTask:destroy()
    oMailman:destroy()
    RETURN
ENDIF

//  Wait for the POP3 connect task to finish.
//  The 1/0 returned by Wait applies to the Wait method call, not the task.
nMaxWaitMs := 30000
nSuccess := oTask:Wait(nMaxWaitMs)
IF (.NOT. nSuccess .OR. (oTask:StatusInt != 7) .OR. (oTask:TaskSuccess != 1))
    IF (.NOT. nSuccess)
        //  The task.LastErrorText applies to the Wait method call.
        ? oTask:LastErrorText
    ELSE
        //  The ResultErrorText applies to the underlying task method call (i.e. the Pop3BeginSession)
        ? oTask:Status
        ? oTask:ResultErrorText
    ENDIF

    oTask:destroy()
    oMailman:destroy()
    RETURN
ENDIF

oTask:destroy()

//  Get the number of messages in the mailbox.
oTask := oMailman:GetMailboxCountAsync()

//  To keep the example short, we'll skip handling failures.
//  The failures would be handled in the same way as shown above.

nSuccess := oTask:Run()
nSuccess := oTask:Wait(nMaxWaitMs)
nNumMessages := oTask:GetResultInt()
oTask:destroy()
IF (nNumMessages == 0)
    oMailman:destroy()
    RETURN
ENDIF

oEmail := CreateObject("Chilkat.Email")

FOR i := 1 TO nNumMessages

    oTask := oMailman:FetchByMsgnumAsync(i)
    IF (oMailman:LastMethodSuccess == 0)
        ? oMailman:LastErrorText
        oMailman:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    nSuccess := oTask:Run()
    nSuccess := oTask:Wait(nMaxWaitMs)
    IF (.NOT. nSuccess .OR. (oTask:StatusInt != 7) .OR. (oTask:TaskSuccess != 1))
        IF (.NOT. nSuccess)
            //  The task.LastErrorText applies to the Wait method call.
            ? oTask:LastErrorText
        ELSE
            //  The ResultErrorText applies to the underlying task method call (i.e. the FetchByMsgnum)
            ? oTask:Status
            ? oTask:ResultErrorText
        ENDIF

        oTask:destroy()
        oMailman:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    //  Each Chilkat object that can be a return value of an asynchronous task will
    //  have a method named LoadTaskResult.  The object returned in the underlying
    //  asynchronous method call is retrieved by calling LoadTaskResult. 

    //  To say it another way: The application will provide a pre-existing object of
    //  the desired return type (in this case it is an email object).  This object is
    //  loaded by calling LoadTaskResult.  

    nSuccess := oEmail:LoadTaskResult(oTask)
    oTask:destroy()

    IF (.NOT. nSuccess)
        ? oEmail:LastErrorText
        oMailman:destroy()
        oEmail:destroy()
        RETURN
    ELSE
        ? oEmail:From + ": " + oEmail:Subject + Chr(10)
    ENDIF

NEXT

oMailman:destroy()
oEmail:destroy()