List of Task working in parallel but not getting Result

Indudhar Gowda 426 Reputation points
2021-12-01T14:47:00.143+00:00

List of Task working in parallel but not getting Result ,,Throwing exception .

var listTask = new List<Task>();
                if (assessmentDatas.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(assessmentDatas)));
                if (adLimits.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(adLimits)));
                if (variables.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(variables)));
                if (resultValues.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(resultValues)));      
                 Task.WaitAll(listTask.ToArray());   

                var results = listTask.Select(x => ((Task<(DataTable dt, string tableName, string tempTableName, Type type)>)x).Result).ToList(); Exception Thrown here

Above Code is Working in Parallel but not able to Get Result.

Below Code I am getting Result, but Not Working in Parallel

  var listTask = new List<Task>();
            if (assessmentDatas.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(assessmentDatas)).Result);
            if (adLimits.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(adLimits)).Result);
            if (variables.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(variables)).Result);
            if (resultValues.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(resultValues)).Result);      

            Task.WaitAll(listTask.ToArray());

            var results = listTask.Select(x => ((Task<(DataTable dt, string tableName, string tempTableName, Type type)>)x).Result).ToList();
Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,633 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,972 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,056 Reputation points
    2021-12-01T21:19:14.173+00:00

    Hi,
    change your code, please:

    var listTask = new List<Task>();
    if (assessmentDatas.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(assessmentDatas)));
    if (adLimits.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(adLimits)));
    if (variables.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(variables)));
    if (resultValues.Count > 0) listTask.Add(Task.Factory.StartNew(() => BulkHelper.GetDataTable(resultValues)));
    Task.WaitAll(listTask.ToArray());
    
    var results = listTask.Select(x => (Task<Task<(DataTable dt, string tableName, string tempTableName, Type type)>>)x).Select(y => y.Result).Select(z => z.Result).ToList();
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful