async/await keywords not available in .net 4.0

using System.Threading.Tasks;

private void simpleMethod()
{
    var tsk = Task.Factory.StartNew(() => DoSomeWorkAsync());
    Task.WaitAll(tsk);
    DataTable table = tsk.Result;
}

It is important that the asynchronous method should not contain any method that affects form controls

    private DataTable DoSomeWorkAsync()
    {           
        System.Data.DataTable table = new System.Data.DataTable();
        Thread.Sleep(4000); // Any long time process
        return table;
    }

More information: https://www.simplethread.com/net-40-and-systemthreadingtasks/


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.


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.

Tags:

C#

Async Await