private void Execute <T>(Func <IDbCommand, Task <T> > commandFunc, DispatchingTaskCompletionSource <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 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(Trace)).ToArray(); if (operations.Length == 0) { DisposeHubs(hubs); return(TaskAsyncHelper.Empty); } var tcs = new DispatchingTaskCompletionSource <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 <IQuery, Task <T> > commandFunc, DispatchingTaskCompletionSource <T> tcs) { using (var session = SessionFactoryInfo.SessionFactory.OpenSession()) { var command = CreateCommand(session); commandFunc(command) .Then(result => tcs.SetResult(result)) .Catch(ex => tcs.SetUnwrappedException(ex), Trace); } }
public virtual Task <int> ExecuteNonQueryAsync() { var tcs = new DispatchingTaskCompletionSource <int>(); return(new Task <int>(() => { int a; try { a = ExecuteNonQuery(); tcs.SetResult(a); return a; } catch (Exception ex) { tcs.SetUnwrappedException(ex); throw; } })); }
private void SendImpl(IEnumerator <IGrouping <string, Message> > enumerator, DispatchingTaskCompletionSource <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); } } }