I have the following workflow that needs to happen in a non-blocking parallel processing manner. I would like the method DoStuff()
to return immediately so I am using Task Parallel Libary
DoStuff():
Do some setup
Parse an Excel file
then for each row
Fill Template with parsed values
Convert filled template to Pdf
Convert pdf to Tiff
when all row processing has completed Create Summary Text File
when summary text file has completed, Finalize
I'm stumbling a bit with the "when all row processing has been completed" step since I want to return immediately. Is the following roughly what I should be doing?
public Task<ProcessingResult> DoStuff() {
return new Task<SetupResult>(SetUp)
.ContinueWith(ParseExcel, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(excelProcessing => {
var templateProcessing = excelProcessing.Result.RowParsing
.Select(template =>
new Task<TemplateFillingResult>(()=>FillTemplate)
.ContinueWith(ConvertToPdf, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(ConvertToTiff, TaskContinuationOptions.OnlyOnRanToCompletion)
).ToArray()
//-------------------------------------------------------------
// This is the part that seems wierd
//-------------------------------------------------------------
Task.Factory.ContinueWhenAll(templateTasks, t=> { }).Wait();
return new TemplatesProcessingResult(templateProcessing);
}, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(CreateSummaryFile, TaskContinuationOptions.OnlyOnRanToCompletion)
.ContinueWith(FinalizeProcessing, TaskContinuationOptions.OnlyOnRanToCompletion);