예제 #1
0
            public async Task <AcquireReadResult> AcquireReadAsync(int desiredItems, CancellationToken ctoken)
            {
                AcquireReadResult parentResult = await parent.AcquireReadAsync(desiredItems, ctoken);

                if (parentResult is AcquireReadSucceeded <T> )
                {
                    AcquireReadSucceeded <T> parentSuccess = (AcquireReadSucceeded <T>)parentResult;
                    ImmutableList <U>        sequence      = ImmutableList <U> .Empty.AddRange(parentSuccess.Items.Select(func));

                    return(new AcquireReadSucceeded <U>(parentSuccess.Offset, sequence));
                }
                else
                {
                    return(parentResult);
                }
            }
예제 #2
0
            public async Task <AcquireReadResult> AcquireReadAsync(int desiredItems, CancellationToken ctoken)
            {
                AcquireReadResult parentResult = await parent.AcquireReadAsync(desiredItems, ctoken);

                if (parentResult is AcquireReadSucceeded <T> )
                {
                    AcquireReadSucceeded <T>        parentSuccess = (AcquireReadSucceeded <T>)parentResult;
                    ImmutableList <Tuple <int, T> > sequence      = ImmutableList <Tuple <int, T> > .Empty;
                    foreach (var x in Enumerable.Range(0, parentSuccess.ItemCount))
                    {
                        sequence = sequence.Add(new Tuple <int, T>(index + x, parentSuccess.Items[x]));
                    }

                    return(new AcquireReadSucceeded <Tuple <int, T> >(parentSuccess.Offset, sequence));
                }
                else
                {
                    return(parentResult);
                }
            }
예제 #3
0
        public static CancellableOperationStarter <V> StartableGet <U, V>(this IQueueSource <U> queue, Func <U, V> convertResult, V eofResult)
        {
            return(delegate(CancellationToken ctoken) {
                CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(ctoken);

                Func <Task <CancellableResult <V> > > taskSource = async delegate {
                    AcquireReadResult arr = await queue.AcquireReadAsync(1, cts.Token);

                    if (arr is AcquireReadSucceeded <U> )
                    {
                        AcquireReadSucceeded <U> succeededResult = (AcquireReadSucceeded <U>)arr;

                        if (succeededResult.ItemCount == 1)
                        {
                            return new GetCancellableResult <U, V>(queue, succeededResult.Items[0], convertResult);
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(succeededResult.ItemCount == 0);
                            return new GetEofCancellableResult <U, V>(queue, eofResult);
                        }
                    }
                    else if (arr is AcquireReadFaulted)
                    {
                        AcquireReadFaulted faultedResult = (AcquireReadFaulted)arr;

                        throw faultedResult.Exception;
                    }
                    else
                    {
                        // ReSharper disable once UnusedVariable
                        AcquireReadCancelled cancelledResult = (AcquireReadCancelled)arr;

                        throw new OperationCanceledException();
                    }
                };

                Task <CancellableResult <V> > task;
                try {
                    task = Task.Run(taskSource);
                }
                catch (OperationCanceledException e) {
#if NET451
                    var tcs = new TaskCompletionSource <CancellableResult <V> >();
                    tcs.SetException(e);
                    tcs.SetCanceled();
                    task = tcs.Task;
#else
                    task = Task.FromCanceled <CancellableResult <V> >(e.CancellationToken);
#endif
                }
                catch (Exception exc) {
#if NET451
                    var tcs = new TaskCompletionSource <CancellableResult <V> >();
                    tcs.TrySetException(exc);
                    task = tcs.Task;
#else
                    task = Task.FromException <CancellableResult <V> >(exc);
#endif
                }

                return new CancellableOperation <V>(task, cts);
            });
        }