问题描述
下面是所涉及的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
isIObservable<TResult> Select<TSource, TResult>(this IObservable<TSource> source, Func<TSource, TResult> selector)
fromSystem.Reactive.Linq.Observable
ToList
isIObservable<IList<TSource>> ToList<TSource>(this IObservable<TSource> source)
fromSystem.Reactive.Linq.Observable
ToTask
isTask<TResult> ToTask<TResult>(this IObservable<TResult> observable)
fromSystem.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是这里。
.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;
}
}
这篇关于空入境奇装异服在任务列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!