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

前端 未结 2 1453
执笔经年
执笔经年 2021-01-12 00:15

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/deta
2条回答
  •  深忆病人
    2021-01-12 00:41

    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!

提交回复
热议问题