// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; namespace System.Threading.Tasks { static class TaskExt { public static readonly Task True = Task.FromResult(true); public static readonly Task False = Task.FromResult(false); public static Task Throw(Exception exception) { var tcs = new TaskCompletionSource(); tcs.TrySetException(exception); return tcs.Task; } public static Task Zip(this Task t1, Task t2, Func f) { var tcs = new TaskCompletionSource(); var i = 2; var complete = new Action(t => { if (Interlocked.Decrement(ref i) == 0) { var exs = new List(); if (t1.IsFaulted) exs.Add(t1.Exception); if (t2.IsFaulted) exs.Add(t2.Exception); if (exs.Count > 0) tcs.TrySetException(exs); else if (t1.IsCanceled || t2.IsCanceled) tcs.TrySetCanceled(); else { var res = default(V); try { res = f(t1.Result, t2.Result); } catch (Exception ex) { tcs.TrySetException(ex); return; } tcs.TrySetResult(res); } } }); t1.ContinueWith(complete); t2.ContinueWith(complete); return tcs.Task; } } }