我的代码:
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 VS2010 到 Async Targeting Pack 定位 .NET 4.0
为了在 VS2010 中使用 async/await,不应使用 Nuget 或通过 .NET 4.5 安装升级的 .NET 4.0(换句话说,安装 .NET 4.5)。
查看我的回答:
- Async CTP not working in VS 2010 SP1
- Where can I find a TPL dataflow version for 4.0
详细介绍如何在 .NET 4.0 中获取和使用 C# 5.0 扩展(未安装 .NET 4.5/VS2012) - Asynchronous Programming with Async and Await
更新:
再次引用"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论坛上也有一个很大的热议:
- "If I target .net 4.0 but run on a machine that has .net 4.5 will .net 4.0 WPF bugs still be there? "
标题有点误导,因为讨论转移到不可能从 VS2012 和/或从装有 .NET 4.5 的机器开发/测试 .NET 4.0(没有安装 .NET 4.5 的机器)
关于c# - 未达到 await 语句后的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240336/