我在Console应用程序中有多个正在运行的任务。在我的任务中,我从EWS Managed API调用了一些方法。所以当EWS抛出异常代码执行时不会返回WaitAll。
while (index < messageData.Count)
{
var mess = messageData[index];
var task = new System.Threading.Tasks.Task<EventsCreationResult>(() =>
{
EventsCreationResult taskResult = null;
taskResult = CreateEmail(mess, Service);
return taskResult;
});
task.Start();
tasks.Add(task);
index = index + 1;
}
System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
和功能
public static EventsCreationResult CreateEmail(string folderName, EmailMessage mess)
{
EventsCreationResult result = new EventsCreationResult();
result.Key = mess.Subject;
try
{
mess.Save(FindFolder(folderName).Id);
result.EventsCreationLog = "Message created:" + mess.Subject;
}
catch (Exception ex)
{
result.Error = ex;
}
return result;
}
那么如何避免死锁,如果它可能不会取消不会引起错误的任务。