' TaskEx'不工作!(BCL)

时间:2016-03-15 06:30:05

标签: asp.net-mvc-4 async-await base-class-library

我是这里的新人。

我有一个复杂的问题。

我正在使用.net 4.0 MVC 4,添加有关Microsoft BLC的参考资料以使用async,等待。

并写了

await System.Threading.Tasks.TaskEx.WhenAll(tl);

for wait线程工作结束当然。 但是这里出现了错误。

Error   13  The type or namespace name 'TaskEx' does not exist in the namespace 'System.Threading.Tasks' (are you missing an assembly reference?)   

我在另一个解决方案上多次测试另一个项目,但它的工作。不喜欢这个。

任何想法都没有帮助?

这里的整个方法。

public async void SendEmailEwsAsync(string subject, string mailBody, string fileName)
    {
        try
        {
            List<System.Threading.Tasks.Task> tl = new List<System.Threading.Tasks.Task>();
            foreach (var kvp in Receivers)
            {
                EmailMessage mail = CreateMailEws(subject, mailBody);

                mail.ToRecipients.Add(kvp.Value);
                if (!this.TestFlag)
                {
                    mail.Attachments.AddFileAttachment(string.Format(fileName, EmailNamePair[kvp.Value]), kvp.Key);
                }

                tl.Add(TaskSendAsync(mail));
                this.CurrentCursor++;
            }
            await System.Threading.Tasks.TaskEx.WhenAll(tl); //error here
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            this.IsEnd = true;
        }
    }

    private System.Threading.Tasks.Task TaskSendAsync(EmailMessage mail)
    {
        Action action = delegate()
        {
            mail.Save(new FolderId(WellKnownFolderName.Drafts, new Mailbox("noreply@whatever.com"))); 
            mail.SendAndSaveCopy(new FolderId(WellKnownFolderName.SentItems, new Mailbox("noreply@whatever.com"))); 
        };
        System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(action);
        task.Start();
        return task;
    }

整个参考资料

Project References List

谢谢你看到这个。

1 个答案:

答案 0 :(得分:-2)

您不需要引用TaskEx,您可以执行WaitAll而不是WhenAll

// Wait for all tasks in list to complete
Task.WaitAll(tl);