确定是否等待任务

本文关键字:任务 等待 是否 | 更新日期: 2023-09-27 18:12:30

如何确定任务是否已经被等待/检查了其结果?

我有一个已创建并激活的背景TaskList,但由于异常行为,可能没有等待。在我的'finally'块中,我需要遍历此列表并await那些尚未等待的任务,以防止在GC时引发潜在的异常。

编辑

我的解决方案:

首先,感谢Steve澄清了我对。net 4.5中如何处理未观察异常的过时理解。这很有趣。但是,我仍然对从调用方法中引发这些异常感兴趣,因此我提出了以下简单的助手类,以提供对不会立即观察到的Task s的轻松管理:

/// <summary>
/// Collection of unobserved tasks.
/// </summary>
public class TaskCollection
{
    /// <summary>
    /// The tasks.
    /// </summary>
    private readonly List<Task> tasks = new List<Task>();
    /// <summary>
    /// Gets a value indicating whether this instance is empty.
    /// </summary>
    /// <value>
    ///   <c>true</c> if this instance is empty; otherwise, <c>false</c>.
    /// </value>
    public bool IsEmpty
    {
        get
        {
            return this.tasks.Count == 0;
        }
    }
    /// <summary>
    /// Adds the specified task.
    /// </summary>
    /// <param name="task">The task.</param>
    /// <returns>The <see cref="task"/>.</returns>
    public Task Add(Task task)
    {
        Contract.Requires(task != null);
        Contract.Requires(!this.Contains(task));
        Contract.Ensures(this.Contains(task));
        Contract.Ensures(Contract.Result<Task>() == task);
        this.tasks.Add(task);
        return task;
    }
    /// <summary>
    /// Adds the specified task.
    /// </summary>
    /// <typeparam name="T">Task return type.</typeparam>
    /// <param name="task">The task.</param>
    /// <returns>The <see cref="task"/>.</returns>
    public Task<T> Add<T>(Task<T> task)
    {
        Contract.Requires(task != null);
        Contract.Requires(!this.Contains(task));
        Contract.Ensures(this.Contains(task));
        Contract.Ensures(Contract.Result<Task<T>>() == task);
        this.tasks.Add(task);
        return task;
    }
    /// <summary>
    /// Determines whether [contains] [the specified task].
    /// </summary>
    /// <param name="task">The task.</param>
    /// <returns>
    /// <c>true</c> if [contains] [the specified task]; otherwise, <c>false</c>.
    /// </returns>
    public bool Contains(Task task)
    {
        Contract.Requires(task != null);
        return this.tasks.Contains(task);
    }
    /// <summary>
    /// Observes the specified task.
    /// </summary>
    /// <param name="task">The task.</param>
    /// <returns>
    /// The task.
    /// </returns>
    public Task When(Task task)
    {
        Contract.Requires(task != null);
        Contract.Requires(this.Contains(task));
        Contract.Ensures(!this.Contains(task));
        this.tasks.Remove(task);
        return task;
    }
    /// <summary>
    /// Observes the specified task.
    /// </summary>
    /// <typeparam name="T">Return type.</typeparam>
    /// <param name="task">The task.</param>
    /// <returns>
    /// The task.
    /// </returns>
    public Task<T> When<T>(Task<T> task)
    {
        Contract.Requires(task != null);
        Contract.Requires(this.Contains(task));
        Contract.Ensures(!this.Contains(task));
        this.tasks.Remove(task);
        return task;
    }
    /// <summary>
    /// Observes all tasks.
    /// </summary>
    /// <returns>The task.</returns>
    public Task WhenAll()
    {
        Contract.Ensures(this.IsEmpty);
        if (this.IsEmpty)
        {
            return TaskEx.FromResult<object>(null);
        }
        var taskArray = this.tasks.ToArray();
        this.tasks.Clear();
        return TaskEx.WhenAll(taskArray);
    }
}

示例用法:

Exception exception;
var tasks = new TaskCollection();
try
{
    var backgroundTask = tasks.Add(DoSomethingAsync());
    // Do something in parallel.
    await tasks.When(backgroundTask);
}
catch (Exception ex)
{
    if (tasks.IsEmpty)
    {
        throw;
    }
    exception = ex;
}
try
{
    await tasks.WhenAll();
}
catch (Exception ex)
{
    exception = new AggregateException(ex, exception);
}
throw new AggregateException(exception);

确定是否等待任务

在我的'finally'块中,我需要遍历这个列表并等待那些尚未等待的任务,以防止在GC时引发潜在的异常。

不,你没有。

在。net 4.5中,未观察到的任务异常不再在结束过程中引发。

你需要小心…一旦事情开始出现问题,就很难保证这项任务能够完成(无论如何)。但是,你可以添加这样的方法:

public static void IgnoreFailure(this Task task) {
    if (task.IsCompleted) { // observe now
        // this is just an opaque method to ensure it gets evaluated
        if (task.IsFaulted) GC.KeepAlive(task.Exception);
    }
    else { // observe in the future
        task.ContinueWith(t => {
            if (t.IsFaulted) GC.KeepAlive(t.Exception);
        }, TaskContinuationOptions.ExecuteSynchronously);
    }
}

,只对每个任务调用t.IgnoreFailure(),无论是否等待。