public void OnCompleted()
 {
     TaskMonitor.Dispose();
     CleanupTasks(null);
     if (CommandTasks.Count > 0)
     {
         Task.WaitAll(CommandTasks.Select(t => (Task)t.Task).ToArray());
     }
 }
        private void CleanupTasks(object state)
        {
            Parallel.ForEach(CommandTasks, t =>
            {
                dynamic ot = null;
                if (CommandTasks.TryTake(out ot))
                {
                    var tsk = (Task)ot.Task;
                    var cmd = (ClientCommand)ot.Command;

                    if (ot.Exception != null)
                    {
                        //Do logging
                    }
                }
            });
        }
 public void OnNext(ClientCommand value)
 {
     lock (TaskLocker)
     {
         CommandTasks.Add(
             new
         {
             Command = value,
             Task    = Task.Run(() =>
             {
                 SagaDependancyFactory.GetCommandExecutor().Execute(value).ContinueWith(result =>
                                                                                        SagaDependancyFactory.GetResultDispatcher().SendResultToServer(result.Result));
                 //throw new NotImplementedException();
             })
         });
     }
 }