How to use async with Visual Studio 2010 and .NET 4.0?

爱⌒轻易说出口 提交于 2019-12-19 05:47:30

问题


I want to add async support to current VS 2010 .NET 4.0 C# project

I have found:

  • Visual Studio Async CTP - http://www.microsoft.com/en-us/download/details.aspx?id=9983
  • Microsoft.Bcl.Async - https://nuget.org/packages/Microsoft.Bcl.Async

I don't even get real difference between them.

I installed both. Visual Studio Async CTP (Version 3), Microsoft.Bcl and Microsoft.Bcl.Async. (also used to run tools\portable-net40+sl4+win8+wp71\install.ps1 in Microsoft.Bcl)

And still can't see any effect. Same error for

public async Task<CommResponse>

->

Error   37  The type or namespace name 'async' could not be found (are you missing a using directive or an assembly reference?)

So is it real how should I use this stuff?


回答1:


I don't think you should do that. The Visual Studio 2010 version of async/await is more of a preview than anything else. If you want to use this in real, production level code, you should definitely upgrade to Visual Studio 2012 (or 2013, if you can wait a little while).

If you don't need this in real production code that requires Visual Studio Pro for some reason, and are just toying around, you can use Visual Studio 2012 Express.




回答2:


I am working on something similar (I'm writing a RestApiLibrary in VS2010, that I borrowed from a VS2017 project) and found the following URL to be helpful.

https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee

The main thing that helped was:

        // Send a request asynchronously continue when complete 
        client.GetAsync(_address).ContinueWith( 
            (requestTask) => 
            { 
                // Get HTTP response from completed task. 
                HttpResponseMessage response = requestTask.Result; 

                // Check that response was successful or throw exception 
                response.EnsureSuccessStatusCode(); 

The 'ContinueWith' method, and the '.Result' property seemed to be key to (sort of using) an async function in VS2010. Note, I doubt this behaves in the traditional async way, but at least this way you can use async methods in VS2010.

Hope this helps!



来源:https://stackoverflow.com/questions/16557032/how-to-use-async-with-visual-studio-2010-and-net-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!