c# – Task.Wait in ContinueWhenAll Action
作者:互联网
我正在努力将线程包含在我的azure代码中,以便将东西放入队列中.要做到这一点我用http://www.microsoft.com/download/en/details.aspx?id=19222作为参考.
我将多条消息排入队列的代码如下所示:
public void AddMessagesAsync(IEnumerable<IQueueMessage> 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<Task>();
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提供的文档).等待你已经知道已经完成的线程似乎有点奇怪吗?我能想象的唯一原因是冒泡并处理错误.我在这里错过了什么吗?
解决方法:
当至少一个Task实例被取消时,Task.WaitAll()
将抛出AggregateException
– 或者 – 在执行至少一个Task实例期间抛出异常.
ContinueWhenAll()
不会抛出此异常,只需在所有内容完成取消后立即开始上一个任务等.
标签:c,azure,task,plinq 来源: https://codeday.me/bug/20190518/1127477.html