1

i am new guy here.

I have a complicate problem.

I am using .net 4.0 MVC 4, add References about Microsoft BLC for using async, await.

and wrote

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

for wait Threads work ends of course. But error pops out here.

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

I tested many times another Project on another Solution, but it's work. not like this.

Any ideas for little help?

whole Method here.

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;
    }

whole References here

Project References List

Thank you for see this.

  • If you mean ASP.NET MVC, you can't use `Microsoft.Bcl.Async` on ASP.NET. Even if you get it to compile (which is possible), it will not behave properly at runtime. – Stephen Cleary Mar 15 '16 at 15:25
  • @StephenCleary for real? Actually, I did add References about MVC in this project, but it's A kind of common library in my another MVC project. so I remove MVC reference, but still doesn't work. – cleanUser_999 Mar 16 '16 at 03:04
  • To be clear, `Microsoft.Bcl.Async` cannot be used on ASP.NET at all. To use `async` on ASP.NET, you have to upgrade to at least .NET 4.5. – Stephen Cleary Mar 16 '16 at 11:23
  • @StephenCleary Yes. but I using `Microsoft.Bcl.Async` from nuget for .NET 4.5 Below. – cleanUser_999 Mar 17 '16 at 02:11

1 Answers1

-2

You do not need to reference TaskEx, and you can do WaitAll instead of WhenAll

// Wait for all tasks in list to complete
Task.WaitAll(tl);
Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • Thx for your comment! one question, is that exactly same work? between TaskEx.WhenAll() and Task.WaitAll() ? – cleanUser_999 Mar 15 '16 at 06:39
  • The WhenAll() method will actually create a new, awaitable task that you can await, while the WaitAll() doesen't. Fine grained difference there, and all depends of course of what you want to. There is a good answer here: http://stackoverflow.com/questions/6123406/waitall-vs-whenall – Pedro G. Dias Mar 15 '16 at 06:45
  • I already have Syncronous method. so I want make Asyncronous one. Seem like WaitAll() is Block type right? but still I thanks for your attention. – cleanUser_999 Mar 15 '16 at 06:51
  • Try without TaskEx. I've never used it, only Task.WhenAll(tl) – Pedro G. Dias Mar 15 '16 at 06:54
  • 1
    Task.WhenAll() is not supported below less than .net 4.5 – cleanUser_999 Mar 15 '16 at 07:02