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

2

10

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

I have found:

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?

Surgery answered 15/5, 2013 at 4:44 Comment(19)
Possible Duplicate : #12057025Hambrick
@Hambrick sorry but I can't find some answer that will help me there. Because they speak there alike Async CTP works for them and it's not for me.Surgery
@Chipzilla thanks but I've got .NET 4.0 set there and I want to have async working with it. And I wonder if it even possible?Surgery
As far as I know, the async keyword needs the VS2012 C# compiler (aka .Net 4.5 compiler) even though you are targeting .Net 4.0, because the compiler needs to understand async. The .Net 4.0 support is a library issue, which is how that can work - but you still need the 4.5 compiler. As far as I know! I used this btw (with VS2012, not VS2010): microsoft.com/en-us/download/details.aspx?id=29576Chura
But Visual Studio Async CTP says it can add such functional to VS 2010.Surgery
@Heather You're right, it does seem to say that (e.g. here). I don't know why this doesn't work for you then. :(Chura
@Heather But it's also a CTP, i.e. code that's not release quality and probably shouldn't be used in production.Ursulaursulette
Did you add a reference to AsyncCTPLibrary.dll? In the Solution Explorer, right-click on your project and choose Add Reference. You should see a 'browse' section and you can add the library through there. You can find it in your Documents folder under Microsoft Visual Studio Async CTP/Samples. You may want to create a new project to test this out, just in case...Thermosphere
well it could be strange but there is no folder Microsoft Visual Studio Async CTP in My DocumentsSurgery
The location of the dll depends on where you installed the Async CTP. I should've mentioned it in my previous comment.Thermosphere
@Chipzilla it doesn't say where installs.Surgery
@Chipzilla same after adding library (got it from nuget...). Does it work for you?Surgery
I haven't attempted it myself as I'm using Visual Studio 2012. But according to one of my books you need the AsyncCTPLibrary in order to use thee async and await keywords in VS 2010.Thermosphere
I think the easiest solution would be to upgrade.Polypus
@Polypus with upgrade you mean buying new Visual Studio? Only if I can explain to my direction why do I need it. And also, there is VS2013 coming so I'm not sure if that is sane to buy 2012 today.Surgery
That was my intention, yes, but, of course, I have no idea what your budget constraints are. I do know that you probably do not want async production code developed with Visual Studio 2010. You want .NET 4.5 and C# 5.Polypus
I'll turn it into an answer and elaboratePolypus
@Heather: If you buy VS2012 with an MSDN subscription, you get VS2013 for free.Telpherage
@StephenCleary Thank you, that's new for me to know but as far as I know MSDN subscription costs a lot.Surgery
P
4

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.

Polypus answered 1/7, 2013 at 9:34 Comment(2)
The Async CTP had many installation problems due to the way the installer worked. @Heather, check out the MSDN forums; missing the Async CTP directory is a sure sign of this. If you installed the CTP right when it was released, it would work, but it breaks if you have other VS updates installed before the CTP. MS is not going to fix this; the only way forward is VS2012.Telpherage
The topic is becoming to complex and going out from actual solution for VS2010 so and I even can't find better solution for myself, I can't search it for ages so I will try to avoid async in 2010. Upgrading to newer version is completely another question for me but I set this answer as solution to close this question.Surgery
P
-1

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!

Presentationism answered 11/9, 2018 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.