Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Async File Processing with Cancellation Support This project demonstrates how to perform asynchronous file processing in Delphi using multi-threading and cancellation tokens. It includes robust error handling and supports user-triggered cancellation during long-running operations. Overview The project consists of three main units:

OperationToken: Implements a cancellation token that tracks the state of an operation. It allows you to:

Fire cancellation or completion events.

Register callbacks to be notified when the operation is canceled or completed.

Query the current status and reason for the cancellation.

Threading.AsyncListProcessing: Provides an asynchronous list processing helper that leverages parallel tasks. This unit:

Processes an array of input items concurrently.

Allows each task to call a user-defined asynchronous processing function.

Supports cancellation via the OperationToken.

Executes a callback on the main thread once results are ready.

Main: Contains a sample VCL form demonstrating:

How to initialize and use the cancellation token.

How to start the asynchronous file processing.

How to cancel the operation via user interaction.

How to process file content and display results.

Features Asynchronous Processing The implementation uses TTask.Run and TParallel.For to execute file processing tasks concurrently, improving performance on multi-core systems.

Cancellation Support The TOperationToken class allows tasks to be canceled gracefully. Callbacks can be registered to react immediately to a cancellation event.

Thread-Safe Design The code uses TCriticalSection to protect shared resources, ensuring safe access from multiple threads.

Callback Mechanism Users can register callbacks that are invoked on the main thread when the operation’s state changes, allowing for smooth UI updates.

Error Handling Exceptions occurring in background tasks are caught and reported via the cancellation token with the osException status.

Delphi Versions: Tested with recent Delphi versions supporting generics and parallel programming.

Installation Clone the Repository

Callback Registration: Callbacks are immediately invoked if the token has already been fired, or stored for later notification.

AsyncListProcessing Unit Generic Async List Processor: Uses a generic class TAsyncListProcessor<TInput, TOutput> to process arrays concurrently.

Await Method: Accepts a callback that is executed on the main thread once each asynchronous task is completed.

Cancellation Check: Each task checks the cancellation token before processing, ensuring resources are not wasted after cancellation.

About

Process list asyn, allow user to cancel and sync results

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages