空入境奇装异服在任务列表中 [英] Strange appearance of a null entry in the list of tasks

查看:172
本文介绍了空入境奇装异服在任务列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是所涉及的code:

Here is the code involved:

private static async Task DoRunInOrderAsync<TTaskSeed>(SemaphoreSlim sem, IObservable<TTaskSeed> taskSeedSource, CreateTaskDelegate<TTaskSeed> createTask, OnTaskErrorDelegate<TTaskSeed> onFailed, OnTaskSuccessDelegate<TTaskSeed> onSuccess) where TTaskSeed : class
{
    var tasks = await taskSeedSource
        .Select(taskSeed => GetPendingOrRunningTask(taskSeed, createTask, onFailed, onSuccess, sem))
        .ToList()
        .ToTask();

    await Task.WhenAll(tasks);
}
private static async Task GetPendingOrRunningTask<T>(T taskSeed, CreateTaskDelegate<T> createTask, OnTaskErrorDelegate<T> onFailed, OnTaskSuccessDelegate<T> onSuccess,
    SemaphoreSlim sem) where T : class
{
    Exception exc = null;
    await sem.WaitAsync();
    try
    {
        var task = createTask(taskSeed);
        if (task != null)
        {
            await task;
        }
        onSuccess(task, taskSeed);
    }
    catch (Exception e)
    {
        exc = e;
    }

    sem.Release();

    if (exc != null)
    {
        onFailed(exc, taskSeed);
    }
}

其中:


  • 选择的IObservable&LT; TResult&GT;选择&LT; TSource,TResult&GT;(此的IObservable&LT; TSource&GT;源,Func键&LT; TSource,TResult&GT;选择器) System.Reactive.Linq.Observable

  • 了ToList 的IObservable&LT;&IList的LT; TSource&GT;&GT; &了ToList LT; TSource&GT;(这与的IObservable LT; TSource&GT;源) System.Reactive.Linq.Observable

  • ToTask 任务&LT; TResult&GT; ToTask&LT; TResult&GT;(这与的IObservable LT; TResult&GT;看到) System.Reactive.Threading.Tasks.TaskObservableExtensions

  • System.Reactive.Linq版本是2.2.5.0

  • Select is IObservable<TResult> Select<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector) from System.Reactive.Linq.Observable
  • ToList is IObservable<IList<TSource>> ToList<TSource>(this IObservable<TSource> source) from System.Reactive.Linq.Observable
  • ToTask is Task<TResult> ToTask<TResult>(this IObservable<TResult> observable) from System.Reactive.Threading.Tasks.TaskObservableExtensions
  • System.Reactive.Linq version is 2.2.5.0

据我所看到的,一切都建成,没有过时的二进制文件身边。误差经常发生,但不总是

As far as I can see, everything is built, no stale binaries around. The error occurs often, but not always.

有关我的生活,我无法理解的任务列表如何包含如果 GetPendingOrRunningTask 方法异步任务

For the life of me, I cannot understand how the tasks list can contain null if the GetPendingOrRunningTask method is async Task ?

修改

所以了ToList 内喷射。怎么样?为什么?我在做什么错了(除了编程为生)?

So ToList injects null. How? Why? What am I doing wrong (besides programming for a living) ?

推荐答案

您可能有一个竞争条件。

You may have a race condition.

.ToList()调用列表&LT; T&GT; 构造函数。在code是<一个href=\"http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,d2ac2c19c9cf1d44\"相对=nofollow>这里。

.ToList() calls the List<T> constructor. The code is here.

如果在时间与你的 taskSeedSource 变化的元素个数的构造开始和结束,这是可能的,你可以用不一致的列表中结束。在94行特定的外观。

If the number of elements in your taskSeedSource changes between the time the constructor starts and finishes, it's possible that you could end up with an inconsistent list. In particular look at line 94.

ICollection<T> c = collection as ICollection<T>;
if( c != null) {
    int count = c.Count;
    if (count == 0)
    {
        _items = _emptyArray;
    }
    else {
        _items = new T[count];
        c.CopyTo(_items, 0);  /* it's possible there are now fewer elements in c */
        _size = count;
    }
} 

这篇关于空入境奇装异服在任务列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆