Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
PDF Signatures
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Async Task Chain

Demonstrates how to combine a sequence of asynchronous tasks into a single task chain to be run in a background thread.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    -- All Chilkat classes can be unlocked at once at the beginning of a program
    -- by calling UnlockBundle.  It requires a Bundle unlock code.
    DECLARE @chilkatGlob int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Global', @chilkatGlob OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int
    EXEC sp_OAMethod @chilkatGlob, 'UnlockBundle', @success OUT, 'Anything for 30-day trial.'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @chilkatGlob, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        RETURN
      END

    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.MailMan', @mailman OUT

    -- Set the POP3 server's hostname
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.someMailServer.com'

    -- Set the POP3 login/password and any other requirements..
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'myLogin'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'
    EXEC sp_OASetProperty @mailman, 'PopSsl', 1
    EXEC sp_OASetProperty @mailman, 'MailPort', 995

    -- Connect to the POP3 server:
    EXEC sp_OAMethod @mailman, 'Pop3BeginSession', @success OUT
    IF Not @success
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    -- Get the number of messages in the mailbox.
    DECLARE @numMessages int
    EXEC sp_OAMethod @mailman, 'GetMailboxCount', @numMessages OUT
    IF @numMessages = 0
      BEGIN

        PRINT 'No email messages in the POP3 mailbox.'
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @mailman
        RETURN
      END

    DECLARE @taskChain int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.TaskChain', @taskChain OUT

    -- Create async task objects to fetch each email message
    -- by its sequence number
    DECLARE @i int

    DECLARE @task int

    SELECT @i = 1
    WHILE @i <= @numMessages
      BEGIN

        EXEC sp_OAMethod @mailman, 'FetchByMsgnumAsync', @task OUT, @i
        EXEC sp_OAGetProperty @mailman, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @chilkatGlob
            EXEC @hr = sp_OADestroy @mailman
            EXEC @hr = sp_OADestroy @taskChain
            RETURN
          END

        -- Append each task to the task chain.
        EXEC sp_OAMethod @taskChain, 'Append', @success OUT, @task
        IF Not @success
          BEGIN
            EXEC sp_OAGetProperty @taskChain, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @chilkatGlob
            EXEC @hr = sp_OADestroy @mailman
            EXEC @hr = sp_OADestroy @taskChain
            RETURN
          END

        EXEC @hr = sp_OADestroy @task

        SELECT @i = @i + 1
      END

    -- 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.
    EXEC sp_OASetProperty @taskChain, 'StopOnFailedTask', 1
    EXEC sp_OAMethod @taskChain, 'Run', @success OUT
    IF Not @success
      BEGIN
        EXEC sp_OAGetProperty @taskChain, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END

    -- 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. 
    EXEC sp_OAGetProperty @taskChain, 'Finished', @iTmp0 OUT
    WHILE @iTmp0 <> 1
      BEGIN

        -- Sleep 100 ms.
        EXEC sp_OAMethod @taskChain, 'SleepMs', NULL, 100

      END

    -- 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:
    EXEC sp_OAGetProperty @taskChain, 'StatusInt', @iTmp0 OUT
    IF @iTmp0 <> 7
      BEGIN

        PRINT 'Task did not complete.'

        EXEC sp_OAGetProperty @taskChain, 'Status', @sTmp0 OUT
        PRINT 'task chain status: ' + @sTmp0
        EXEC @hr = sp_OADestroy @chilkatGlob
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @taskChain
        RETURN
      END

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Email', @email OUT

    DECLARE @numTasks int
    EXEC sp_OAGetProperty @taskChain, 'NumTasks', @numTasks OUT
    DECLARE @taskIdx int
    SELECT @taskIdx = 0

    WHILE (@taskIdx < @numTasks)
      BEGIN

        EXEC sp_OAMethod @taskChain, 'GetTask', @task OUT, @taskIdx

        -- Load the email object with email downloaded by this task.
        EXEC sp_OAMethod @email, 'LoadTaskResult', @success OUT, @task
        IF Not @success
          BEGIN

            PRINT 'Failed to load email object for task.'
          END
        ELSE
          BEGIN
            EXEC sp_OAGetProperty @email, 'From', @sTmp0 OUT

            EXEC sp_OAGetProperty @email, 'Subject', @sTmp1 OUT
            PRINT @sTmp0 + '; ' + @sTmp1
          END

        EXEC @hr = sp_OADestroy @task

        SELECT @taskIdx = @taskIdx + 1
      END

    EXEC @hr = sp_OADestroy @chilkatGlob
    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @taskChain
    EXEC @hr = sp_OADestroy @email


END
GO

 

© 2000-2021 Chilkat Software, Inc. All Rights Reserved.