代码等待后声明中没有达到
我的代码:代码等待后声明中没有达到
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工作。 我想知道为什么不工作在Windows 7上使用VS 2010年它
链接到包含我的代码库:https://bitbucket.org/chovik/smartlib-mu-wp
我已经下载了.zip文件的解决方案,但它包括了包的DLL通过的NuGet得到的,即DLL文件从VS2012针对.NET 4.0和从可运行机器仅安装了.NET 4.5。人们也可以不通过Async Targeting Pack
使用C#5.0扩展从VS 2012 上VS2010靶向.NET 4.0为了使用异步/ AWAIT在VS2010一个不应该使用的NuGet或.NET 4.0由.NET 4.5安装(升级换句话说,要安装.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细节
更新:
一旦agai N,从"Proper way to use Async with VS 2010 now that VS 2012 is released"
援引答案“的异步CTP是在Visual Studio中使用异步2010 然而唯一的办法,它不是使它成为.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的机器))/ /或从.NET 4.5机器
你确定吗?我正在开发Windows Phone 7.1的应用程序,而不是桌面。 – Michal 2013-04-26 18:00:21
好的。也许你是对的。我正在使用这个[library](http://nuget.org/packages/Microsoft.Bcl.Async/),它不适用于Visual Studio 2010. – Michal 2013-04-26 18:25:53
谢谢。我用AsyncCtpLibrary_Phone取代了Microsoft.Bcl.Async。也许现在它不会使用Visual Studio 2012.我会看到:)。 – Michal 2013-04-26 18:40:42
你肯定“GetResponseAsyncEx”的实际执行?你能告诉我们它的源代码吗?
这只是一个完整的猜测,但可能值得一试。
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);
return client.GetResponseAsyncEx<List<Book>>(request);
}
很有可能是因为不等待包含对LoadRecentlyRatedBooks
的调用的方法而导致死锁。例如您ReloadCommand
吸附材料(在SmartLib \ MVVM \的ViewModels \ ServerBooksViewModel.cs)就是这样的候选
注意,我不能在VS打开项目由于某种原因,所以我只扫描在记事本文件 - 有可能是其他这样的事件。
退房斯蒂芬·克利里的与异步死锁帖子/等待
我会尝试抛出你的'catch'消息框调试支持'异步'在VS201中0非常差。 – 2013-04-26 15:48:49
什么文件/文件夹包含该代码(在您的存储库中)? – 2013-04-26 16:47:38
https://bitbucket.org/chovik/smartlib-mu-wp/src/d7e3a7ee3d44/SmartLib/BookManager.cs?at=master方法LoadRecentlyRatedBooks,LoadTopRatedBooks。 https://bitbucket.org/chovik/smartlib-mu-wp/src/d7e3a7ee3d44/SmartLib/RequestManagers/BookRequests.cs?at=master方法GetBooksByCategory。 – Michal 2013-04-26 17:09:15