-1

I have this code currently where each task calls an API:

                asyncTasks = new List<Task>
                {
                    itemDetailsOperation.Import(),
                    importPromotionsOperation.Update(),
                    importCripTransactionHistoryOperation.Import(),
                    importShelfTagTransactionHistoryOperation.Import(),
                    importClaimTransactionHistoryOperation.Import(),
                    importNaturalClaimTransactionHistoryOperation.Import(),
                    importPriceAuditTransactionHistoryOperation.Import(),
                    importOrdersOperation.Import(),
                    importAdNotificationsOperation.Import(),
                    importScheduledShipmentListOperation.Import(),
                    importDeliveryDepartmentOperation.Import(),
                    importDeliveryScheduleOperation.Import(),
                    importFutureShipmentItemsOperation.Import(),
                    importOrderGuidesOperation.Import(),
                    importClaimReasonCodeOperation.Import()
                };
                await Task.WhenAll(asyncTasks.ToArray());

And instead of throwing an exception (current logic) when one fails I would like to know which ones had a failed API call.

I tried looking at other related topics and tried doing the following code:

        public class BaseTask : Task
        {
            public Boolean succeed;

            public BaseTask()
            {
            }
        }

but the compiler doesn't like it because "'Task' does not contain a constructor that takes 0 arguments". My thought was that once the API fails I can set the succeed flag to false and if it succeeds I can set it to true.

Then once all the tasks are complete I can retrieve all ones that failed.

Just looking for any thoughts or guidance!

  • 3
    You know tasks actually have properties, right? You could throw a `try` and `catch` around your `WhenAll` line, and then in the catch iterate through and find the faulted task. – ProgrammingLlama Dec 14 '21 at 04:35
  • 2
    Does this answer your question? [Is it possible to get successful results from a Task.WhenAll when one of the tasks fails?](https://stackoverflow.com/questions/55887028/is-it-possible-to-get-successful-results-from-a-task-whenall-when-one-of-the-tas) – GSerg Dec 14 '21 at 04:35
  • 1
    Tasks already have the [`IsCompletedSuccessfully`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.iscompletedsuccessfully) property. Why add another property (`succeed`) that means the same thing? – Theodor Zoulias Dec 14 '21 at 10:14

1 Answers1

0

If you inherit Task, then you have to call at least one of constructors of Task in constructor of BaseTask. For example:

    public class BaseTask : Task
    {
        public Boolean succeed;

        public BaseTask(Action action) : base(action)
        {
        }
    }

There are more constructors, that can be helpful for you and compilator.

Leemellaret
  • 197
  • 1
  • 9