internal static Task Continuation <TResult>(this Task <TResult> previous, MarshalableTaskCompletionSource <TResult> tcs) { return(previous.ContinueWith(t => { if (t.IsCanceled) { tcs.SetCanceled(); } else if (t.IsFaulted) { tcs.SetException(t.Exception.InnerExceptions.ToArray()); } else { tcs.SetResult(t.Result); } })); }
/// <summary> /// Invokes the target asynchronous function. /// </summary> /// <typeparam name="T1"> /// First argument type. /// </typeparam> /// <typeparam name="TResult"> /// The result type. /// </typeparam> /// <param name="domain"> /// The domain to invoke the function in. /// </param> /// <param name="arg1"> /// The first argument. /// </param> /// <param name="toInvoke"> /// The function to invoke. /// </param> /// <returns> /// The Task result. /// </returns> public static Task <TResult> InvokeAsync <T1, TResult>(AppDomain domain, T1 arg1, Func <T1, Task <TResult> > toInvoke) { if (domain == null) { throw new ArgumentNullException(nameof(domain)); } if (toInvoke == null) { throw new ArgumentNullException(nameof(toInvoke)); } var proxy = Remote <RemoteFuncAsync <T1, TResult> > .CreateProxy(domain); var tcs = new MarshalableTaskCompletionSource <TResult>(); proxy.RemoteObject.Invoke(arg1, toInvoke, tcs); return(tcs.Task); }