private static Task RunTask(Task task, Action successor) { var tcs = new TaskCompletionSource <object>(); task.ContinueWithPreservedCulture(t => { if (t.IsFaulted) { tcs.SetUnwrappedException(t.Exception); } else if (t.IsCanceled) { tcs.SetCanceled(); } else { try { successor(); tcs.SetResult(null); } catch (Exception ex) { tcs.SetUnwrappedException(ex); } } }); return(tcs.Task); }
internal static Task <TResult> RunTask(Task task, Func <TResult> successor) { var tcs = new TaskCompletionSource <TResult>(); task.ContinueWithPreservedCulture(t => { if (t.IsFaulted) { tcs.SetUnwrappedException(t.Exception); } else if (t.IsCanceled) { tcs.SetCanceled(); } else { try { tcs.SetResult(successor()); } catch (Exception ex) { tcs.SetUnwrappedException(ex); } } }); return(tcs.Task); }
internal static Task RunTask(Task <T> task, Action <T> successor) { var tcs = new TaskCompletionSource <object>(); task.ContinueWith(t => { if (t.IsFaulted) { tcs.SetUnwrappedException(t.Exception); } else if (t.IsCanceled) { tcs.SetCanceled(); } else { try { successor(t.Result); tcs.SetResult(null); } catch (Exception ex) { tcs.SetUnwrappedException(ex); } } }); return(tcs.Task); }
internal static Task <T> FromError <T>(Exception e) { var tcs = new TaskCompletionSource <T>(); tcs.SetUnwrappedException <T>(e); return(tcs.Task); }
public static Task Then(this Task[] tasks, Action successor) { if (tasks.Length == 0) { return(FromMethod(successor)); } var tcs = new TaskCompletionSource <object>(); Task.Factory.ContinueWhenAll(tasks, completedTasks => { var faulted = completedTasks.FirstOrDefault(t => t.IsFaulted); if (faulted != null) { tcs.SetUnwrappedException(faulted.Exception); return; } var cancelled = completedTasks.FirstOrDefault(t => t.IsCanceled); if (cancelled != null) { tcs.SetCanceled(); return; } successor(); tcs.SetResult(null); }); return(tcs.Task); }
private Task ExecuteHubEvent(IRequest request, string connectionId, Func <IHub, Task> action) { var hubs = GetHubs(request, connectionId).ToList(); var operations = hubs.Select(instance => action(instance).OrEmpty().Catch()).ToArray(); if (operations.Length == 0) { DisposeHubs(hubs); return(TaskAsyncHelper.Empty); } var tcs = new TaskCompletionSource <object>(); Task.Factory.ContinueWhenAll(operations, tasks => { DisposeHubs(hubs); var faulted = tasks.FirstOrDefault(t => t.IsFaulted); if (faulted != null) { tcs.SetUnwrappedException(faulted.Exception); } else if (tasks.Any(t => t.IsCanceled)) { tcs.SetCanceled(); } else { tcs.SetResult(null); } }); return(tcs.Task); }
private void Execute <T>(Func <IDbCommand, Task <T> > commandFunc, TaskCompletionSource <T> tcs) { IDbConnection connection = null; try { connection = _dbProviderFactory.CreateConnection(); connection.ConnectionString = ConnectionString; var command = CreateCommand(connection); connection.Open(); commandFunc(command) .Then(result => tcs.SetResult(result)) .Catch(ex => tcs.SetUnwrappedException(ex), Trace) .Finally(state => { var conn = (DbConnection)state; if (conn != null) { conn.Dispose(); } }, connection); } catch (Exception) { if (connection != null) { connection.Dispose(); } throw; } }
private static Task RunTaskSynchronously(Task task, Action <object> next, object state, bool onlyOnSuccess = true) { var tcs = new TaskCompletionSource <object>(); task.ContinueWithPreservedCulture(t => { try { if (t.IsFaulted) { if (!onlyOnSuccess) { next(state); } tcs.SetUnwrappedException(t.Exception); } else if (t.IsCanceled) { if (!onlyOnSuccess) { next(state); } tcs.SetCanceled(); } else { next(state); tcs.SetResult(null); } } catch (Exception ex) { tcs.SetUnwrappedException(ex); } }, TaskContinuationOptions.ExecuteSynchronously); return(tcs.Task); }
public static void ContinueWithNotComplete(this Task task, TaskCompletionSource <object> tcs) { task.ContinueWithPreservedCulture(t => { if (t.IsFaulted) { tcs.SetUnwrappedException(t.Exception); } else if (t.IsCanceled) { tcs.SetCanceled(); } }, TaskContinuationOptions.NotOnRanToCompletion); }
private void Execute <T>(Func <IDbCommand, Task <T> > commandFunc, TaskCompletionSource <T> tcs, CommandType commandType) { IDbConnection connection = null; try { connection = _dbProviderFactory.CreateConnection(); connection.ConnectionString = ConnectionString; var command = CreateCommand(connection); command.CommandType = commandType; connection.Open(); commandFunc(command) .Then(result => tcs.SetResult(result)) .Catch((exception, o) => { tcs.SetUnwrappedException(exception); }, Trace) .Finally(state => { var conn = (DbConnection)state; if (conn != null) { conn.CloseConnection(); conn.Dispose(); } }, connection); } catch (Exception ex) { Trace.TraceError("Error during connection open. Details: {0}", ex.ToString()); if (connection != null) { connection.CloseConnection(); connection.Dispose(); } throw; } }
private void SendImpl(IEnumerator <IGrouping <string, Message> > enumerator, TaskCompletionSource <object> taskCompletionSource) { send: if (!enumerator.MoveNext()) { taskCompletionSource.TrySetResult(null); } else { IGrouping <string, Message> group = enumerator.Current; // Get the channel index we're going to use for this message int index = (int)((uint)_sipHashBasedComparer.GetHashCode(group.Key) % StreamCount); Debug.Assert(index >= 0, "Hash function resulted in an index < 0."); Task sendTask = StreamManager.Send(index, group.ToArray()).Catch(_logger); if (sendTask.IsCompleted) { try { sendTask.Wait(); goto send; } catch (Exception ex) { taskCompletionSource.SetUnwrappedException(ex); } } else { sendTask.Then((enumer, tcs) => SendImpl(enumer, tcs), enumerator, taskCompletionSource) .ContinueWithNotComplete(taskCompletionSource); } } }
private void SendImpl(IEnumerator<IGrouping<string, Message>> enumerator, TaskCompletionSource<object> taskCompletionSource) { send: if (!enumerator.MoveNext()) { taskCompletionSource.TrySetResult(null); } else { IGrouping<string, Message> group = enumerator.Current; // Get the channel index we're going to use for this message int index = (int)((uint)_sipHashBasedComparer.GetHashCode(group.Key) % StreamCount); Debug.Assert(index >= 0, "Hash function resulted in an index < 0."); Task sendTask = StreamManager.Send(index, group.ToArray()).Catch(_trace); if (sendTask.IsCompleted) { try { sendTask.Wait(); goto send; } catch (Exception ex) { taskCompletionSource.SetUnwrappedException(ex); } } else { sendTask.Then((enumer, tcs) => SendImpl(enumer, tcs), enumerator, taskCompletionSource) .ContinueWithNotComplete(taskCompletionSource); } } }