2
votes

I want my Task to complete before I perform other actions. If I use

Task.WaitAll(task);

it certainly blocks my UI thread. What is the best way to wait asynchronously for a Task to complete, without using async / await (I'm using .NET v4.0)?

1
The concept of waiting asynchronously is hurting my brain...Matthew Watson
Why not just run it synchronously?doctorlove
You can only compose continuations (ContinueWith). That being said, you can use async & await and target .NET 4. You have to use VS2012 though, with Async Targeting Pack.Patryk Ćwiek
what's the point of making asynchronous calls if it's to wait for them to complete. It's same thing as taking a 1 lane road split it to 8 lane and come back 1 lane.Franck
Hmm, if you really mean "how do I call a method when a certain task has completed", then @user2834880 has the right answer.Matthew Watson

1 Answers

7
votes

You can use async / await also in .NET 4.0 when you add the Microsoft.Bcl.Async NuGet package to your project. Besides this, you have not much choice - either you use asynchronous calls or your block your thread with the method you mentioned already.

However, you're not bound to await when using asynchronous method. You can also use a callback:

task.ContinueWith(r => {
    /* code */
});