Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Async Task ChainDemonstrates how to combine a sequence of asynchronous tasks into a single task chain to be run in a background thread.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). 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 -- Use "Chilkat_9_5_0.Global" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.MailMan" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.TaskChain" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.Email" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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-2024 Chilkat Software, Inc. All Rights Reserved.