I am creating a number of tasks in a loop like so and adding them to a List.
for (int startThreads = 0; startThreads < beginThreadsPerDS; startThreads++)
{
var extDbCnn_threaded = new SqlConnection(extDbConnectionString);
var srcDbCnn_threaded = new SqlConnection(srcDbConnectionString);
var queryTasks = Task.Run(() => ExtractDataSetCode_Query(extDbCnn_threaded, srcDbCnn_threaded, strTmpDsc, strPatList, row, tbOutputPath.Text, strReportScope, strOutFormat, strDataSetType, loggingEnabled, /*dataSetNum,*/ lstDataSetCdTasks));
lock (LstDataSetCdTasks)
{
lstDataSetCdTasks.SetQueryTasksForDataSetCode(strTmpDsc, queryTasks);
}
}
I then create a task that completes when all of the above complete:
var queryWaitTask = Task.WhenAll(lstDataSetCdTasks.GetQueryTasksByDataSetCode(strTmpDsc));
So that I can use that task to add the continuation task below, which should only run once all the "Query Tasks" have completed:
var queryReassignmentTask = queryWaitTask.ContinueWith(antecedent => ExtractDataSetCode_assignTasksToNextDS(extDbCnn, srcDbCnn, strTmpDsc, strPatList, row, tbOutputPath.Text, strReportScope, strOutFormat, strDataSetType, loggingEnabled, lstDataSetCdTasks), TaskContinuationOptions.OnlyOnRanToCompletion);
My problem is that at times, in the "ExtractDataSetCode_Query" method (signature shown below) that is called by each of the original tasks I spawn, I get an error thrown from one of the stored procedures that is called in that method. When that happens, I want to be able to handle it and notify the user that an error occurred in one of the tasks (as opposed to what happens now is that the faulted task just exits/stops running the ExtractDataSetCode_Query method while the others keep going. I am only made aware that anything has happended once the last Task completes (which can be hours or days later). I want to know that a task has exited/faulted when it happens. Everything I am finding about how to handle these situations mentions creating a continuation task that continues "OnlyOnFaulted", however, given how I am spawning all these tasks then storing them in a List and THEN waiting on them based on the Task.WhenAll, I just dont see how that can work. What is the best way to accomplish this? Thanks!
private void ExtractDataSetCode_Query(SqlConnection extDbCnn, SqlConnection srcDbCnn, String extDataSetCode, String patList, DataGridViewRow currRow, String strOutputPath, String strRptScope, String strOutFmt, String strDataSetTyp, String strLogEnable, LstDataSetCdTasks lstDataSetCdTasks)
``