Task.Wait in ContinueWhenAll Action

Я работал над включением потоков в свой лазурный код для помещения вещей в очередь. для этого я использовал http://www.microsoft.com/download/en/details.aspx?id=19222 в качестве ссылки.

мой код для постановки в очередь нескольких сообщений выглядит так:

public void AddMessagesAsync(IEnumerable messages, string queue = null, TimeSpan? timeToLive = null)
{
  //check if we need to switch queues
  if (!String.IsNullOrEmpty(queue))
  {
    SetCurrent(queue);
  }

  //setup list of messages to enqueue
  var tasks = new List();
  Parallel.ForEach(messages, current => {
    if (timeToLive.HasValue)
    {
      //create task with TPL
      var task = Task.Factory.FromAsync(Current.BeginAddMessage, Current.EndAddMessage, Convert(current), timeToLive.Value, tasks); 
      //setup continuation to trigger eventhandler
      tasks.Add(task.ContinueWith((t) => AddMessageCompleted(t)));
    }
    else
    {
      //create task with TPL
      var task = Task.Factory.FromAsync(Current.BeginAddMessage, Current.EndAddMessage, Convert(current), tasks);
      //setup continuation to trigger eventhandler
      tasks.Add(task.ContinueWith((t) => AddMessageCompleted(t)));
    }
  });

  //setup handler to trigger when all messages are enqueued, a we are blocking the thread over there to wait for all the threads to complete
  Task.Factory.ContinueWhenAll(tasks.ToArray(), (t) => AddMessagesCompleted(t));               
}

private void AddMessagesCompleted(Task[] tasks)
{
  try
  {
    //wait for all tasks to complete
    Task.WaitAll(tasks);
  }
  catch (AggregateException e)
  {
    //log the exception
    var ex = e;
    //return ex;
  }

  if (AddedMessages != null)
  {
    AddedMessages(tasks, EventArgs.Empty);
  }
}

Теперь мой вопрос касается Task.Wait в продолжении (что соответствует документу, предоставленному MS). кажется немного странным ждать потоков, которые, как вы уже знаете, завершились, верно? единственная причина, по которой я могу представить, - это выдавить ошибки и обработать их. я что-то упустил?

5
задан Dan Atkinson 13 April 2012 в 12:00
поделиться