c# - 未达到 await 语句后的代码

标签 c# visual-studio-2010 windows-phone-7 async-await async-ctp

我的代码:

public async Task LoadRecentlyRatedBooks()
{
    RecentlyRatedBooks.Clear();
    try
    {
        var books = await App.CurrentApplication
                             .BookRequests
                             .GetBooksByCategory(BookListCategory.News, 10, 0);
        if (books != null)
        {
            foreach (var book in books)
            {
                var bookViewModel = AddBook(book);

                if (bookViewModel != null)
                {
                    RecentlyRatedBooks.Add(bookViewModel);
                }
            }
        }
    }
    catch(Exception ex)
    {         }
}

public async Task<IEnumerable<Book>> 
         GetBooksByCategory(BookListCategory category, uint limit, uint offset)
{
    var request = CreateBookListURL(category, limit, offset);
    var response = await client.GetResponseAsyncEx<List<Book>>(request);
    return response.Data;
}

我在 await 语句后调用代码时遇到问题。我的应用程序到达了 GetBooksByCategory() 方法中的 return 语句,但它从未到达 await 语句之后的代码。我尝试将断点置于 catch block 或包含 if (books != null) 的行,但没有成功。应用程序永远不会到达这些断点。

我现在在 Windows 7 上使用 Visual Studio 2010。还安装了 Visual Studio 2012 for Desktop。

我确定我的代码可以在 Windows 8 和 Visual Studio 2012 for WP 上运行。 我想知道为什么它不能在带有 VS 2010 的 Windows 7 上运行。

包含我的代码的存储库链接:https://bitbucket.org/chovik/smartlib-mu-wp

最佳答案

我已经下载了您的 .zip 解决方案,但它包括通过 Nuget 获得的包中的 dll,即用于从 VS2012 定位 .NET 4.0 的 dll,并且只能从安装了 .NET 4.5 的机器上运行。也不能使用 C# 5.0 扩展从 VS 2012 VS2010Async Targeting Pack 定位 .NET 4.0

为了在 VS2010 中使用 async/await,不应使用 Nuget 或通过 .NET 4.5 安装升级的 .NET 4.0(换句话说,安装 .NET 4.5)。

查看我的回答:

更新:
再次引用"Proper way to use Async with VS 2010 now that VS 2012 is released"的回答

"The Async CTP is the only way to use async in Visual Studio 2010. However, it is not the async that made it into .NET 4.5 / Visual Studio 2012"

MSDN论坛上也有一个很大的热议:

关于c# - 未达到 await 语句后的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240336/

相关文章:

c# - Linq to object 中的多个 where 或条件

c# - 从文件 c# 添加到注册表项

c# - .NET 标签中的文本格式

c# - 错误 : Cannot find all types required by the 'async' modifier. 您是否针对错误的框架版本,或缺少对程序集的引用?

c# - 将查询参数从强类型 View 传递回 Controller

c# - 编写单元测试 : How to get folder with testfiles programmatically

silverlight - 应用程序退出后独立存储应用程序设置不会保留

c# - 在 Windows Phone 7 或 Windows Phone 8 中使用 CollectionView 的 LongListSelector 分组

c# - 在 Access 数据库之间复制表

asp.net-mvc - 识别 Visual Studio 选项卡列表中的 View