Await alternative in .NET 4.0?

前端 未结 2 1782
生来不讨喜
生来不讨喜 2021-01-04 18:32

What would be the best alternative for the await keyword in .NET 4.0 ? I have a method which needs to return a value after an asynchronous operation. I noticed the wait() me

2条回答
  •  隐瞒了意图╮
    2021-01-04 18:44

    I think your basic options are

    • Using Task and .ContinueWith()
    • Using the Async CTP and async / await
    • Using Reactive Extensions

    The easiest way is probably to install the Async CTP. As far as I know the license allows comercial usage. It patches the compiler and comes with a 150kb dll that you can include into your project.

    You can use Task and .ContinueWith(). But that means, that you have to take some effort with exeption handling and flow control.

    Tasks are a functional construct. That's why ContinueWith() does not mix well with imperative constructs like for loops or try-catch blocks. Therefore async and await got introduced, so that the compiler can help us out.

    If you can't have that support of the compiler (i.e. you use .Net 4.0), your best bet is to use the TAP together with a functional framework. Reactive Extensions is a very good framework to treat asynchronous methods.

    Just google for "reactive extensions tasks" to get started.

提交回复
热议问题