Esempio n. 1
0
        public static Task <TResult> Then <T, TResult>(this Task <T> task, Func <T, Task <TResult> > onFulfilled)
        {
            task.NotNull(nameof(task));
            onFulfilled.NotNull(nameof(onFulfilled));

            if (task.IsCompleted)
            {
                if (task.Exception?.InnerException != null)
                {
                    return(Promise.Reject <TResult>(task.Exception.InnerExceptions));
                }
                if (task.IsCanceled)
                {
                    return(Promise.Cancel <TResult>());
                }

                try
                {
                    return(onFulfilled(task.Result));
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    return(Promise.Reject <TResult>(e));
                }
            }

            var tcs = new TaskCompletionSource <TResult>();

            task.ContinueWith(t =>
            {
                if (t.Exception?.InnerException != null)
                {
                    tcs.SetException(t.Exception.InnerExceptions);
                    return;
                }

                if (t.IsCanceled)
                {
                    tcs.SetCanceled();
                    return;
                }

                Task <TResult> fulfilledTask;
                try
                {
                    fulfilledTask = onFulfilled(t.Result);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    tcs.SetException(e);
                    return;
                }

                if (fulfilledTask.IsCompleted)
                {
                    tcs.SetFromCompleted(fulfilledTask);
                    return;
                }

                fulfilledTask.ContinueWith(
                    ft => tcs.SetFromCompleted(ft), CancellationToken.None,
                    TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

            return(tcs.Task);
        }
Esempio n. 2
0
    private void StartListen()
    {
        // Start the receive task, that will remain running for the whole connection
        System.Threading.CancellationToken ct = stopServerTokenSource.Token;
        var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            ct.ThrowIfCancellationRequested();
            while (true)
            {
                try
                {
                    System.Threading.Tasks.Task <Response> receiveTask = ReceiveMessage();
                    receiveTask.Wait();

                    if (receiveTask.Result.MessageId == Messages.EVENT)
                    {
                        ProcessEvent(receiveTask.Result);
                    }
                    else if (taskCompletion != null)
                    {
                        taskCompletion.SetResult(receiveTask.Result);
                    }
                    else
                    {
                        throw new ErrorException("Received WAMP message that we did not expect.");
                    }

                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }
                }
                catch (System.Exception e)
                {
                    if (e.InnerException.GetType() == typeof(System.Net.WebSockets.WebSocketException))
                    {
                        var exception = e.InnerException as System.Net.WebSockets.WebSocketException;
                        if (exception.WebSocketErrorCode == System.Net.WebSockets.WebSocketError.ConnectionClosedPrematurely)
                        {
                            if (taskCompletion != null)
                            {
                                taskCompletion.SetException(e);
                            }

                            OnDisconnect();

                            return;
                        }
                    }

                    if (ws.State != System.Net.WebSockets.WebSocketState.Open)
                    {
                        OnDisconnect();
                        return;
                    }


                    // Signal the exception to the other thread and continue to listen
                    if (taskCompletion != null)
                    {
                        taskCompletion.SetException(e);
                    }
                }
            }
        }, stopServerTokenSource.Token);
    }
Esempio n. 3
0
 /// <summary>
 /// Transitions the underlying task into the <see cref="SystemTasks.TaskStatus.Faulted"/> state
 /// and binds a collection of exception objects to it.
 /// </summary>
 public static void SetException(SystemTaskCompletionSource tcs,
                                 IEnumerable <Exception> exceptions) =>
 tcs.SetException(exceptions);
Esempio n. 4
0
 /// <summary>
 /// Transitions the underlying task into the <see cref="SystemTasks.TaskStatus.Faulted"/> state
 /// and binds it to a specified exception.
 /// </summary>
 public static void SetException(SystemTaskCompletionSource tcs, Exception exception) =>
 tcs.SetException(exception);
        public static Task <T> Finally <T>(this Task <T> task, Func <Task> onFinally)
        {
            task.NotNull(nameof(task));
            onFinally.NotNull(nameof(onFinally));

            TaskCompletionSource <T> tcs;

            if (task.IsCompleted)
            {
                Task finallyTask;
                try
                {
                    finallyTask = onFinally();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    return(Promise.Reject <T>(e));
                }

                if (finallyTask.IsCompleted)
                {
                    if (finallyTask.Exception?.InnerException != null)
                    {
                        return(Promise.Reject <T>(finallyTask.Exception.InnerExceptions));
                    }

                    return(finallyTask.IsCanceled ? Promise.Cancel <T>() : task);
                }

                tcs = new TaskCompletionSource <T>();

                finallyTask.ContinueWith(ft =>
                {
                    if (ft.Exception?.InnerException != null)
                    {
                        tcs.SetException(ft.Exception.InnerExceptions);
                        return;
                    }

                    if (ft.IsCanceled)
                    {
                        tcs.SetCanceled();
                        return;
                    }

                    tcs.SetFromCompleted(task);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

                return(tcs.Task);
            }

            tcs = new TaskCompletionSource <T>();

            task.ContinueWith(t =>
            {
                Task finallyTask;
                try
                {
                    finallyTask = onFinally();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    tcs.SetException(e);
                    return;
                }

                if (finallyTask.IsCompleted)
                {
                    if (finallyTask.Exception?.InnerException != null)
                    {
                        tcs.SetException(finallyTask.Exception.InnerExceptions);
                        return;
                    }

                    if (finallyTask.IsCanceled)
                    {
                        tcs.SetCanceled();
                        return;
                    }

                    tcs.SetFromCompleted(t);
                    return;
                }

                finallyTask.ContinueWith(ft =>
                {
                    if (ft.Exception?.InnerException != null)
                    {
                        tcs.SetException(ft.Exception.InnerExceptions);
                        return;
                    }

                    if (ft.IsCanceled)
                    {
                        tcs.SetCanceled();
                        return;
                    }

                    tcs.SetFromCompleted(t);
                }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
            }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

            return(tcs.Task);
        }