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
(DataFlex) 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.
Use ChilkatAx-9.5.0-win32.pkg Procedure Test Variant vStreamA Handle hoStreamA Handle hoStreamB Boolean iSuccess Variant vTaskA Handle hoTaskA Variant vTaskB Handle hoTaskB Handle hoFac Boolean iBFilesEqual String sTemp1 String sTemp2 Boolean bTemp1 Get Create (RefClass(cComChilkatStream)) To hoStreamA If (Not(IsComObjectCreated(hoStreamA))) Begin Send CreateComObject of hoStreamA End Get Create (RefClass(cComChilkatStream)) To hoStreamB If (Not(IsComObjectCreated(hoStreamB))) Begin Send CreateComObject of hoStreamB End Set ComSourceFile Of hoStreamA To "qa_data/hamlet.xml" // streamA will feed into streamB. (streamA is the source for streamB) Get pvComObject of hoStreamA to vStreamA Get ComSetSourceStream Of hoStreamB vStreamA To iSuccess Set ComSinkFile Of hoStreamB To "qa_output/hamlet_copy.xml" // Run each stream asynchronously in background threads: Get ComRunStreamAsync Of hoStreamA To vTaskA If (IsComObject(vTaskA)) Begin Get Create (RefClass(cComChilkatTask)) To hoTaskA Set pvComObject Of hoTaskA To vTaskA End Get ComRunStreamAsync Of hoStreamB To vTaskB If (IsComObject(vTaskB)) Begin Get Create (RefClass(cComChilkatTask)) To hoTaskB Set pvComObject Of hoTaskB To vTaskB End // Start each task. (The call to Run returns immediately. // each task begins running in its own background thread.) Get ComRun Of hoTaskA To iSuccess Get ComRun Of hoTaskB To iSuccess // 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. While (((ComFinished(hoTaskA)) <> True) Or ((ComFinished(hoTaskB)) <> True)) Send ComSleepMs To hoTaskB 20 Loop // 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? Get ComTaskSuccess Of hoTaskA To bTemp1 If (bTemp1 <> True) Begin Showln "streamA failed:" Get ComResultErrorText Of hoTaskA To sTemp1 Showln sTemp1 Move False To iSuccess End // Did streamB succeed in writing the entire file? Get ComTaskSuccess Of hoTaskB To bTemp1 If (bTemp1 <> True) Begin Showln "streamB failed:" Get ComResultErrorText Of hoTaskB To sTemp1 Showln sTemp1 Move False To iSuccess End If (iSuccess <> True) Begin Procedure_Return End // Let's double-check to see that the files are equal in size and content: Get Create (RefClass(cComCkFileAccess)) To hoFac If (Not(IsComObjectCreated(hoFac))) Begin Send CreateComObject of hoFac End Get ComSourceFile Of hoStreamA To sTemp1 Get ComSinkFile Of hoStreamB To sTemp2 Get ComFileContentsEqual Of hoFac sTemp1 sTemp2 To iBFilesEqual If (iBFilesEqual <> True) Begin Showln "The output file is not equal to the input file!" End Else Begin Showln "The file was successfully copied in a very roundabout way!" End End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.