[英]Inheriting from Task in C#
I have this code currently where each task calls an API:我目前有这个代码,其中每个任务调用一个 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.而不是在失败时抛出异常(当前逻辑),我想知道哪些有失败的 API 调用。
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".但编译器不喜欢它,因为“'Task' 不包含带 0 个参数的构造函数”。 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.
我的想法是,一旦 API 失败,我可以将成功标志设置为 false,如果成功,我可以将其设置为 true。
Then once all the tasks are complete I can retrieve all ones that failed.然后,一旦所有任务完成,我就可以检索所有失败的任务。
Just looking for any thoughts or guidance!只是寻找任何想法或指导!
If you inherit Task
, then you have to call at least one of constructors of Task
in constructor of BaseTask
.如果继承
Task
,则必须在BaseTask
的构造函数中调用至少一个Task
的构造函数。 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.还有更多的构造函数,对你和编译器都有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.