![]() |
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
(Classic ASP) 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.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <% ' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.Stream") set streamA = Server.CreateObject("Chilkat.Stream") ' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.Stream") set streamB = Server.CreateObject("Chilkat.Stream") streamA.SourceFile = "qa_data/hamlet.xml" ' streamA will feed into streamB. (streamA is the source for streamB) success = streamB.SetSourceStream(streamA) streamB.SinkFile = "qa_output/hamlet_copy.xml" ' Run each stream asynchronously in background threads: ' taskA is a Chilkat.Task Set taskA = streamA.RunStreamAsync() ' taskB is a Chilkat.Task Set taskB = streamB.RunStreamAsync() ' Start each task. (The call to Run returns immediately. ' each task begins running in its own background thread.) success = taskA.Run() success = taskB.Run() ' 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. Do While ((taskA.Finished <> 1) Or (taskB.Finished <> 1)) taskB.SleepMs 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? If (taskA.TaskSuccess <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( "streamA failed:") & "</pre>" Response.Write "<pre>" & Server.HTMLEncode( taskA.ResultErrorText) & "</pre>" success = 0 End If ' Did streamB succeed in writing the entire file? If (taskB.TaskSuccess <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( "streamB failed:") & "</pre>" Response.Write "<pre>" & Server.HTMLEncode( taskB.ResultErrorText) & "</pre>" success = 0 End If If (success <> 1) Then Response.End End If ' Let's double-check to see that the files are equal in size and content: ' For versions of Chilkat < 10.0.0, use CreateObject("Chilkat_9_5_0.FileAccess") set fac = Server.CreateObject("Chilkat.FileAccess") bFilesEqual = fac.FileContentsEqual(streamA.SourceFile,streamB.SinkFile) If (bFilesEqual <> 1) Then Response.Write "<pre>" & Server.HTMLEncode( "The output file is not equal to the input file!") & "</pre>" Else Response.Write "<pre>" & Server.HTMLEncode( "The file was successfully copied in a very roundabout way!") & "</pre>" End If %> </body> </html> |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.