Search code examples
.netvisual-studio-2010visual-studioasync-ctpasync-await

Using Async V3 without installing it


I've problem installing VS-SP1 and Async CTP V3 on my machine. For some reason it cause any .NET application I run on that machine to crash. (good thing I'm using virtual machine and I can roll-back).

I've copy the update csc.exe file into .NET framework 4 directory in my Windows directory.

When I build my project with msbuild from command line everything build and works great.

When I compile the same project from within VS2010 I get compile time error that the async keyword is unknown.

I don't care that VS does not highlight the syntax of async and await but it does bother me that I have to build the project from command line.

  1. How come the command line csc works but when using VS it does not?

  2. How can I change VS to compile the async code properly (without installing Async CTP itself)?

Thank you,

Ido.


Solution

  • VS2012 and the Async targeting pack is the best option, for the following reasons:

    1. The syntax is the same as in C#5, making upgrade to C#5 easier. The Async CTP has enough differences (TaskEx instead of Task classes, TaskAsync instead of Task suffixes, token parameters etc), that make upgrade a nuisance. Not difficult, just a nuisance
    2. Await/async behavior is much better and closer to C#5. In the Async CTP, an exception that occurs inside a loop will cause the entire loop to silently fail. See Retry a task for a scenario that is affected by this.
    3. There are no installation issues. The Async CTP and VS 2010 SP1 had to be installed in a certain order to avoid problems.
    4. You can expect some sort of support from MS for the targeting pack, while you can expect none for the Async pack.

    A small caveat is that installing VS2012 on a machine with VS2010 + Async CTP breaks the Async CTP. You won't be able to use the VS2010 version to compile Async code anymore.

    As an example where the async behavior is important, consider the following very simple way of retrying a task. While the function compiles with the Async CTP, it fails SILENTLY (as in application termination silently) if an exception occurs:

    private static async Task<T> Retry<T>(Func<T> func, int retryCount)
    {
        while (true)
        {
            try
            {
                var result = await Task.Run(func);
                return result;
            }
            catch
            {
                if (retryCount == 0)
                    throw;
                retryCount--;
            }
        }
    }
    

    That said, I've been using the Async CTP for over a year now and the only issue was the installation order after adding Visual Studio 2010 SP1. If you get the order wrong the Asycn CTP won't install and you will have to remove SP1 and reinstall everything in the proper order.

    Still, I would move to 2012 and the targeting pack as soon as is practical (ie the entire team is ready to switch), to avoid multiple code versions and the annoying exception behavior.