Task in all versions of the the .Net framework and .Net Standard 2.0 has a useful property missing. From .Net Standard 2.1 and through all versions of .Net Core the "IsCompletedSuccessfully" property is available. This will tell the use that the task completed without throwing an exeception or because it was cancelled. Well we can use an extension to make up for this missing property.
namespace System.Threading.Tasks { public static class TaskIsCompletedSuccessfullyExtension { public static bool IsCompletedSuccessfully(this Task task) { var value = task.IsCompleted && !(task.IsCanceled || task.IsFaulted); return value; } } }and to use:
using System.Threading.Tasks; ... Task myTask = ... // Note it is defined not as a property but as a method if (myTask.IsCompletedSuccessfully()) { ... }
No comments:
Post a Comment