Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

async/await keywords not available in .net 4.0

Tags:

I would like to use the async/await in C# 4.0 and I have installed the following package:

http://www.nuget.org/packages/Microsoft.Bcl.Async/

The problem is that I do not have async/await keywords available.

I am using VS2010 with SP1.

Thanks.

like image 406
Álvaro García Avatar asked Oct 17 '13 09:10

Álvaro García


People also ask

Is await keyword blocked?

The await keyword, by contrast, is non-blocking, which means the current thread is free to do other things during the wait.

What is async and await keyword in C#?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete. await can only be used inside an async method.

Is async await blocking C#?

The await operator doesn't block the thread that evaluates the async method. When the await operator suspends the enclosing async method, the control returns to the caller of the method.

How does the await keyword work in C#?

The await keyword in C# programming language is used to suspend all async methods enclosed until the point where the operations presented by the asynchronous method are completed. In order or a developer to call multiple functions in an asynchronous way, async and await are highly used and recommended.


2 Answers

You're not going to get a better answer than Jon Skeet's.

The only supported way to do this is to use VS2012 with Microsoft.Bcl.Async.

VS2010 is very difficult to get working with async/await. There was an old Async CTP package (which had many bugs that were never fixed) that acted as an "add-on"/"partial replacement" to VS2010. However, that package never worked well with the VS2010 updates. So, you'd have to first find a version of one of the old CTP installers, play around with installing some VS updates, and then see if the CTP works. If you've already installed all your VS2010 updates then no version of the CTP will work. I think once you find an update situation where you can install a working CTP, then you can install the other updates.

After all this work, you'll still end up with a bug-ridden (and definitely unoptimized) implementation of async.

Or, you can do as Jon Skeet suggested and download the free version of VS2012 Express with Microsoft.Bcl.Async and have a fully supported solution.

like image 61
Stephen Cleary Avatar answered Sep 22 '22 17:09

Stephen Cleary


Async/Await have been introduced with C# 5.0 and .NET Framework 4.5

more information here:

Asynchronous Programming with Async and Await (C# and Visual Basic)

Asynchronous Programming in C# 5.0 using async and await

If you are using Framework 4 as I do in enterprise you can use other workarounds. You can use a NuGet package that allows you to use these features.

Using async/await without .NET Framework 4.5

Just install it from NuGet package manager:

Install-Package Microsoft.Bcl.Async

Extracted from NuGet Gallery:

This package enables Visual Studio 2012 projects to use the new 'async' and 'await' keywords. This package also includes Task-based extension methods that allow using some of the existing asynchronous APIs with the new language keywords. Windows Phone Silverlight 8 projects can use this package to get access to async extension methods for the networking types.

like image 22
Carlos Landeras Avatar answered Sep 21 '22 17:09

Carlos Landeras