Suppose I have a processor whose task is to save files to disk. This runs as a Task , observing the BlockingCollection<T> for the files being processed.
When a task is canceled and there are still files that need to be saved to disk, what would be good practice for this?
It would be convenient if the task right before the output quickly wrote the remaining files to disk, although I am not sure that this contradicts the philosophy of canceling the task (since cancellation should be performed as quickly as possible).
Another option is the second procedure after the cancellation of the task, the task of which is to write the remaining files to disk.
Code example:
class FileProcessor { private readonly BlockingCollection<Stream> input; public FileProcessor(BlockingCollection<Stream> input) { _input = input; } public Task Run(CancellationToken cancellationToken, BlockingCollection<Stream> input) { return Task.Factory.StartNew(() => { foreach (Stream stream in input.GetConsumingEnumerable(cancellationToken)) { WriteToDisk(stream); }
I know this is a slightly open question, the application / requirements / number of files to write also play a role, but I'm looking for general recommendations / best practices here.
Polity
source share