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 Chain (another example)Demonstrates using a task chain to run a sequence of FTP tasks asynchronously.
-- 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) -- 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 @ftp int -- Use "Chilkat_9_5_0.Ftp2" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Ftp2', @ftp OUT EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.example.com' EXEC sp_OASetProperty @ftp, 'Username', 'login' EXEC sp_OASetProperty @ftp, 'Password', 'password' -- Connect and login to the FTP server. EXEC sp_OAMethod @ftp, 'Connect', @success OUT IF @success <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @chilkatGlob EXEC @hr = sp_OADestroy @ftp 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 a task to change to the remote directory where the file will be uploaded. DECLARE @task int EXEC sp_OAMethod @ftp, 'ChangeRemoteDirAsync', @task OUT, 'junk' EXEC sp_OAGetProperty @ftp, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @chilkatGlob EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @taskChain RETURN END -- Add this task to the task chain. EXEC sp_OAMethod @taskChain, 'Append', @success OUT, @task EXEC @hr = sp_OADestroy @task -- Create a task to upload a file. DECLARE @localFilename nvarchar(4000) SELECT @localFilename = 'c:/temp/hamlet.xml' DECLARE @remoteFilename nvarchar(4000) SELECT @remoteFilename = 'hamlet.xml' EXEC sp_OAMethod @ftp, 'PutFileAsync', @task OUT, @localFilename, @remoteFilename EXEC sp_OAGetProperty @ftp, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 = 0 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @chilkatGlob EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @taskChain RETURN END -- Add this task to the task chain. EXEC sp_OAMethod @taskChain, 'Append', @success OUT, @task EXEC @hr = sp_OADestroy @task -- 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 @ftp EXEC @hr = sp_OADestroy @taskChain RETURN END -- The application is now free to do anything else -- while the FTP commands are being run... -- 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 @ftp EXEC @hr = sp_OADestroy @taskChain RETURN END -- If we got to this point, the ChangeRemoteDir and PutFile were successful. -- We can visually verify by examining the LastErrorText that was recorded for each -- of these method calls.. 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 -- Examine the status of this task, and the ResultErrorText -- (the ResultErrorText is the ftp.LastErrorText captured for FTP method called by the task). -- Everything should indicate success. EXEC sp_OAGetProperty @task, 'Status', @sTmp0 OUT PRINT 'task status: ' + @sTmp0 EXEC sp_OAGetProperty @task, 'ResultErrorText', @sTmp0 OUT PRINT 'task log: ' + @sTmp0 EXEC @hr = sp_OADestroy @task SELECT @taskIdx = @taskIdx + 1 END EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT EXEC @hr = sp_OADestroy @chilkatGlob EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @taskChain END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.