Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Chaining Asynchronous StreamsThe asynchronous functionality of Chilkat, combined with streams, can offer some interesting possibilities. This example demonstrates how to create two streams, call them streamA and streamB, where streamA feeds into streamB and both streams run in background threads. The examples demonstrates copying a file in this way: sourceFile --> streamA --> streamB --> outputFile This may seem pointless, but the possibilities become interesting when intermediate streams can perform tasks such as encryption or compression, or when a source or sink is a socket, a TLS connection, an SSH tunnel, etc. Or perhaps if a stream becomes the source for the file in an HTTP upload, or perhaps it is the source or sink for files in FTP, SFTP, etc. This is where Chilkat's Async standards in combination with Chilkat Stream can begin to offer powerful and simple-to-implement possibilities.
-- 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 DECLARE @iTmp1 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @sTmp1 nvarchar(4000) DECLARE @streamA int -- Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Stream', @streamA OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @streamB int -- Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Stream', @streamB OUT EXEC sp_OASetProperty @streamA, 'SourceFile', 'qa_data/hamlet.xml' -- streamA will feed into streamB. (streamA is the source for streamB) DECLARE @success int EXEC sp_OAMethod @streamB, 'SetSourceStream', @success OUT, @streamA EXEC sp_OASetProperty @streamB, 'SinkFile', 'qa_output/hamlet_copy.xml' -- Run each stream asynchronously in background threads: DECLARE @taskA int EXEC sp_OAMethod @streamA, 'RunStreamAsync', @taskA OUT DECLARE @taskB int EXEC sp_OAMethod @streamB, 'RunStreamAsync', @taskB OUT -- Start each task. (The call to Run returns immediately. -- each task begins running in its own background thread.) EXEC sp_OAMethod @taskA, 'Run', @success OUT EXEC sp_OAMethod @taskB, 'Run', @success OUT -- Wait for each task to complete. -- Note: The SleepMs method is a utility method that puts the caller to sleep -- for a specified number of milliseconds. -- It is the caller's thread that sleeps (i.e. this foreground thread). -- It does not cause the background thread of the given task object to sleep. EXEC sp_OAGetProperty @taskA, 'Finished', @iTmp0 OUT EXEC sp_OAGetProperty @taskB, 'Finished', @iTmp1 OUT WHILE ((@iTmp0 <> 1) or (@iTmp1 <> 1)) BEGIN EXEC sp_OAMethod @taskB, 'SleepMs', NULL, 20 END -- At this point, each background task has finished. -- Let's check to see if each stream succeeded. -- Did streamA succeed in reading the entire file? EXEC sp_OAGetProperty @taskA, 'TaskSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN PRINT 'streamA failed:' EXEC sp_OAGetProperty @taskA, 'ResultErrorText', @sTmp0 OUT PRINT @sTmp0 SELECT @success = 0 END -- Did streamB succeed in writing the entire file? EXEC sp_OAGetProperty @taskB, 'TaskSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN PRINT 'streamB failed:' EXEC sp_OAGetProperty @taskB, 'ResultErrorText', @sTmp0 OUT PRINT @sTmp0 SELECT @success = 0 END IF @success <> 1 BEGIN EXEC @hr = sp_OADestroy @streamA EXEC @hr = sp_OADestroy @streamB RETURN END -- Let's double-check to see that the files are equal in size and content: DECLARE @fac int -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT DECLARE @bFilesEqual int EXEC sp_OAGetProperty @streamA, 'SourceFile', @sTmp0 OUT EXEC sp_OAGetProperty @streamB, 'SinkFile', @sTmp1 OUT EXEC sp_OAMethod @fac, 'FileContentsEqual', @bFilesEqual OUT, @sTmp0, @sTmp1 IF @bFilesEqual <> 1 BEGIN PRINT 'The output file is not equal to the input file!' END ELSE BEGIN PRINT 'The file was successfully copied in a very roundabout way!' END EXEC @hr = sp_OADestroy @streamA EXEC @hr = sp_OADestroy @streamB EXEC @hr = sp_OADestroy @fac END GO |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.