Beispiel #1
0
        public static Task AsGenericTaskException(this Exception exception, Type type)
        {
            var tcs = TaskCompletionSourceGeneric.Create(type);

            tcs.SetException(exception);
            return(tcs.Task);
        }
Beispiel #2
0
        public static Task AsGenericTaskResult(this object result, Type type)
        {
            var tcs = TaskCompletionSourceGeneric.Create(type);

            tcs.SetResult(result);
            return(tcs.Task);
        }
Beispiel #3
0
        public static Task CastResultAs <T>(this Task <T> task, Type resultTypeToCastTo)
        {
            var tcs = TaskCompletionSourceGeneric.Create(resultTypeToCastTo);

            task.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    tcs.SetException(t.Exception);
                }
                else if (t.IsCanceled)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    try
                    {
                        tcs.SetResult(t.Result);
                    }
                    catch (InvalidCastException ex)
                    {
                        tcs.SetException(ex);
                    }
                }
            }, TaskContinuationOptions.ExecuteSynchronously);
            return(tcs.Task);
        }
 public static TaskCompletionSourceGeneric AsTCSGeneric(this Type type, object state) => TaskCompletionSourceGeneric.Create(type, state);