async/await keywords not available in .net 4.0

前端 未结 4 562
清歌不尽
清歌不尽 2020-12-03 04:39

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 t

相关标签:
4条回答
  • 2020-12-03 05:20
    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/

    0 讨论(0)
  • 2020-12-03 05:25

    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.

    0 讨论(0)
  • 2020-12-03 05:34

    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.

    0 讨论(0)
  • 2020-12-03 05:39

    I have written a .NET 4.5 plugin using async for a .NET 4.0 application, and to my surprise this actually worked!

    I think this worked because I have .NET 4.5 installed, which replaced the .NET 4 runtime with an updated one, which is used both for .NET 4.0 and .NET 4.5. Then my plugin was loaded with reflection using Assembly.Load(...) or similar. I tried both async/await and Environment.CurrentManagedThreadId (a .NET 4.5 property), and both worked.

    Thus if installing .NET 4.5 is an option (i.e. not supporting Windows XP) one could possibly use a mix by loading the .NET 4.5 part of an application via reflection, possibly letting your 4.5 part implement a 4.0 interface. However, if you are writing the application yourself, then a much simpler solution would be to switch to .NET 4.5 for the whole project.

    0 讨论(0)
提交回复
热议问题