Async/Await Keywords Not Available in .Net 4.0

async/await keywords not available in .net 4.0

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.

async not working with Visual Studio 2010 and .NET 4.0?

async and await have been introduced in C# 5. Visual Studio 2010 supports C# 4. Thats why you can't compile code with async/await under VS 2010.

C# async and await keywords not working as expected with property getter

When the compiler encounters await keyword it will automatically schedule a task in task scheduler. The operation waits (in a non-blocking manner) for the task to complete before continue to do the rest of the code block.

To make your code run in parallel you need to modify it into

public async Task GetDistance()
{
var leftDTask = Task.Run(() => LeftDistance = SensorLeft.Distance);
var rightDTask= Task.Run(() => RightDistance = SensorRight.Distance);
await Task.WhenAll(leftDTask,rightDTask);
Distance = RightDistance < LeftDistance ? RightDistance:LeftDistance;
}

Task.WhenAll has an overload to return Task<TResult[]> Task.WhenAll<TResult[]>. and you can replace your code into

public async Task GetDistance()
{
var leftDTask = Task.Run(() => SensorLeft.Distance);
var rightDTask= Task.Run(() => SensorRight.Distance);
var results= await Task.WhenAll(leftDTask,rightDTask);
Distance = Math.Min(results[0],results[1]);
}

However, this is not a good solution. If you want the get distance truly asynchronous, you should make an asynchronous get method rather wrap it into Task.Run.

How can I use the async keywords in a project targeting.net 4.0

You want the Microsoft.Bcl.Async package. That's a properly released, non-CTP package that is stable. This also requires VS2012 since an updated compiler is needed to understand async and await.

In theory one could use older tools from VS2010 along with the older CTP library, I'd strongly recommend that you don't - it has bugs and the installation itself is a hit-or-miss.

Creating an async method in .NET 4.0 that can be used with await in .NET 4.5

As others have stated, you start by having a method return Task or Task<TResult>. This is sufficient to await its result in .NET 4.5.

To have your method fit in as well as possible with future asynchronous code, follow the guidelines in the Task-based Asynchronous Pattern document (also available on MSDN). It provides naming conventions and parameter recommendations, e.g., for supporting cancellation.

For the implementation of your method, you have a few choices:

  • If you have existing IAsyncResult-based asynchronous methods, use Task.Factory.FromAsync.
  • If you have another asynchronous system, use TaskCompletionSource<TResult>.

Async / await with task.run vs task.run and continuewith in .NET 4.0

It depends on what you're doing with ContinueWith. But yes, often you can use await to achieve the same effects you'd previously have achieved using ContinueWith. What you can't do is things like "continue with this code only on failure" - you just use normal exception handling for that. As AlexH says, there will be further differences in terms of the overall behaviour of your async method - but in most cases I'd say that's desirable. Basically, the asynchrony of the code flows, so async methods tend to call more async methods, etc.

I suggest you read up on what async/await is about (there are loads of resources out there - I'd recommend the "Consuming the Task-based Asynchronous Pattern" page on MSDN as one starting point.



Related Topics



Leave a reply



Submit