Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3/4 Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(PureBasic) Chaining Asynchronous Streams
The 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.
IncludeFile "CkTask.pb" IncludeFile "CkStream.pb" IncludeFile "CkFileAccess.pb" Procedure ChilkatExample() streamA.i = CkStream::ckCreate() If streamA.i = 0 Debug "Failed to create object." ProcedureReturn EndIf streamB.i = CkStream::ckCreate() If streamB.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStream::setCkSourceFile(streamA, "qa_data/hamlet.xml") ; streamA will feed into streamB. (streamA is the source for streamB) success.i = CkStream::ckSetSourceStream(streamB,streamA) CkStream::setCkSinkFile(streamB, "qa_output/hamlet_copy.xml") ; Run each stream asynchronously in background threads: taskA.i = CkStream::ckRunStreamAsync(streamA) taskB.i = CkStream::ckRunStreamAsync(streamB) ; Start each task. (The call to Run returns immediately. ; each task begins running in its own background thread.) success = CkTask::ckRun(taskA) success = CkTask::ckRun(taskB) ; 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 ((CkTask::ckFinished(taskA) <> 1) OR (CkTask::ckFinished(taskB) <> 1)) CkTask::ckSleepMs(taskB,20) Wend ; 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 CkTask::ckTaskSuccess(taskA) <> 1 Debug "streamA failed:" Debug CkTask::ckResultErrorText(taskA) success = 0 EndIf ; Did streamB succeed in writing the entire file? If CkTask::ckTaskSuccess(taskB) <> 1 Debug "streamB failed:" Debug CkTask::ckResultErrorText(taskB) success = 0 EndIf If success <> 1 CkStream::ckDispose(streamA) CkStream::ckDispose(streamB) ProcedureReturn EndIf ; Let's double-check to see that the files are equal in size and content: fac.i = CkFileAccess::ckCreate() If fac.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bFilesEqual.i = CkFileAccess::ckFileContentsEqual(fac,CkStream::ckSourceFile(streamA),CkStream::ckSinkFile(streamB)) If bFilesEqual <> 1 Debug "The output file is not equal to the input file!" Else Debug "The file was successfully copied in a very roundabout way!" EndIf CkStream::ckDispose(streamA) CkStream::ckDispose(streamB) CkFileAccess::ckDispose(fac) ProcedureReturn EndProcedure
© 2000-2019 Chilkat Software, Inc. All Rights Reserved.