2 /* -----------------------------------------------------------------------
3 * <copyright file="TaskExtensions.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Collections.Concurrent;
44 using System.Collections.Generic;
45 using System.Diagnostics.Contracts;
48 using System.Threading;
49 using System.Threading.Tasks;
53 public static class TaskExtensions
55 public static Task<T2> Then<T1, T2>(this Task<T1> first, Func<T1, Task<T2>> next)
58 throw new ArgumentNullException("first");
60 throw new ArgumentNullException("next");
61 Contract.EndContractBlock();
62 return Then(first, next, CancellationToken.None);
65 public static Task Then<T1>(this Task<T1> first, Func<T1, Task> next)
68 throw new ArgumentNullException("first");
70 throw new ArgumentNullException("next");
71 Contract.EndContractBlock();
72 return Then(first, next, CancellationToken.None);
75 public static Task<T2> Then<T1, T2>(this Task<T1> first, Func<T1, Task<T2>> next, CancellationToken cancellationToken)
78 throw new ArgumentNullException("first");
80 throw new ArgumentNullException("next");
81 Contract.EndContractBlock();
82 Contract.Assume(TaskScheduler.Current!=null);
84 var tcs = new TaskCompletionSource<T2>();
85 first.ContinueWith(delegate
87 if (first.IsFaulted) tcs.TrySetException(first.Exception.InnerExceptions);
88 else if (first.IsCanceled) tcs.TrySetCanceled();
93 var t = next(first.Result);
94 if (t == null) tcs.TrySetCanceled();
95 else t.ContinueWith(delegate
97 if (t.IsFaulted) tcs.TrySetException(t.Exception.InnerExceptions);
98 else if (t.IsCanceled) tcs.TrySetCanceled();
99 else tcs.TrySetResult(t.Result);
100 }, TaskContinuationOptions.ExecuteSynchronously);
102 catch (Exception exc) { tcs.TrySetException(exc); }
104 }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current);
108 public static Task Then<T1>(this Task<T1> first, Func<T1, Task> next, CancellationToken cancellationToken)
111 throw new ArgumentNullException("first");
113 throw new ArgumentNullException("next");
114 Contract.EndContractBlock();
115 Contract.Assume(TaskScheduler.Current != null);
117 var tcs = new TaskCompletionSource<object>();
118 first.ContinueWith(delegate
120 if (first.IsFaulted) tcs.TrySetException(first.Exception.InnerExceptions);
121 else if (first.IsCanceled) tcs.TrySetCanceled();
126 var t = next(first.Result);
127 if (t == null) tcs.TrySetCanceled();
128 else t.ContinueWith(delegate
130 if (t.IsFaulted) tcs.TrySetException(t.Exception.InnerExceptions);
131 else if (t.IsCanceled) tcs.TrySetCanceled();
132 else tcs.TrySetResult(null);
133 }, TaskContinuationOptions.ExecuteSynchronously);
135 catch (Exception exc) { tcs.TrySetException(exc); }
137 }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current);
143 public static void ReportExceptions(this Task task,Action<AggregateException> OnError,Action OnSuccess )
145 if (!task.IsCompleted) throw new InvalidOperationException("The task has not completed.");
147 task.Exception.Handle(exc=>
149 OnError(task.Exception);
158 public static bool TryRemove<T>(this ObservableConcurrentCollection<T> collection,T item) where T:class
160 IProducerConsumerCollection<T> items= collection;
161 for (var i = 0; i < items.Count; i++)
164 if (!items.TryTake(out tempItem))
166 if (tempItem == item)
173 public static bool TryAdd<T>(this ObservableConcurrentCollection<T> collection,T item) where T:class
175 if (collection==null)
176 throw new ArgumentNullException("collection");
177 Contract.EndContractBlock();
182 IProducerConsumerCollection<T> items= collection;
183 return items.TryAdd(item);