3

My Code:

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;
}

I have problem with calling code after await statement. My application reaches the return statement in GetBooksByCategory() method but it never reaches the code after await statement. I tried put breakpoint to catch-block or to line containing if (books != null) but without success. Application never reaches these breakpoints.

Im now using Visual Studio 2010 on Windows 7. There is also Visual Studio 2012 for Desktop installed.

Im sure that my code is working on Windows 8 with Visual Studio 2012 for WP. I wonder why is it not working on Windows 7 with VS 2010.

Link to repository containing my code: https://bitbucket.org/chovik/smartlib-mu-wp

Michal
  • 343
  • 1
  • 5
  • 16
  • I would try tossing up a message box in your `catch`. Debugging support for `async` in VS2010 was very poor. – Stephen Cleary Apr 26 '13 at 15:48
  • What file/folder contains that code (in your repository)? – G. Stoynev Apr 26 '13 at 16:47
  • https://bitbucket.org/chovik/smartlib-mu-wp/src/d7e3a7ee3d44/SmartLib/BookManager.cs?at=master methods LoadRecentlyRatedBooks, LoadTopRatedBooks. https://bitbucket.org/chovik/smartlib-mu-wp/src/d7e3a7ee3d44/SmartLib/RequestManagers/BookRequests.cs?at=master method GetBooksByCategory. – Michal Apr 26 '13 at 17:09
  • I have added MessageBox after await statement and into catch block. It did not help. No Message Box shown :/. – Michal Apr 26 '13 at 17:28
  • I took a look at your project, but couldn't find anything obviously wrong. Could you produce a minimal repro? – Stephen Cleary Apr 26 '13 at 18:31

3 Answers3

1

I've downloaded your .zip solution but it includes dlls in packages got through Nuget, i.e. dlls for targeting .NET 4.0 from VS2012 and runnable from from machine with installed .NET 4.5 only. One also cannot use C# 5.0 extensions for targeting .NET 4.0 from VS 2012 on VS2010 through Async Targeting Pack

In order to use async/await in VS2010 one should not use Nuget or .NET 4.0 upgraded by .NET 4.5 installation (in other words, to have .NET 4.5 installed).

See my answers to:

Update:
Once again, citing the answer from "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"

There was also a big hot discussion in MSDN forums:

Community
  • 1
  • 1
  • Are you sure? I'm developing application for Windows Phone 7.1, not for Desktop. – Michal Apr 26 '13 at 18:00
  • Ok. Maybe you are right. I'm using this [library](http://nuget.org/packages/Microsoft.Bcl.Async/), which does not work with Visual Studio 2010. – Michal Apr 26 '13 at 18:25
  • Thank you. I have replaced Microsoft.Bcl.Async with AsyncCtpLibrary_Phone. Maybe now it will not work using Visual Studio 2012. I will see :). – Michal Apr 26 '13 at 18:40
  • @Michail, I've never tried .NET 4.5/VS2012 which is impossible on Windows XP. And I have no reasons to migrate to incompatible .NET 4.5/VS2012 since my customer is on Windows XP/VS2010. I've re-checked many times that Nuget brings nonworking/incompatible C# 5.0 extensions dlls (in packets) from VS2010 while I succeeded to use them through Async CTP download. My understanding was to use SEPARATE machines for working with async/await 1)from VS2010 and 2)from VS2012. At least, to install Async CTP **FIRST**. Would be interesting to have feedback if/how you've succeded the other way around – Gennady Vanin Геннадий Ванин Apr 27 '13 at 05:23
0

Are you sure that "GetResponseAsyncEx" is actually executing? Can you show us the source code for it?

This is just a complete guess but might be worth a go.

   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);
}
pingoo
  • 2,074
  • 14
  • 17
  • yes I'm sure. response.Data contains data downloaded from server. – Michal Apr 26 '13 at 16:02
  • Source code: https://bitbucket.org/chovik/smartlib-mu-wp/src/d7e3a7ee3d44089f2596c4a06fa356055b2566bb/SmartLib/RequestManagers/RestClientExtensions.cs?at=master – Michal Apr 26 '13 at 16:04
0

Most likely you are causing a deadlock by not awaiting a method that contains a call to LoadRecentlyRatedBooks. For example your ReloadCommand getter (in SmartLib\MVVM\ViewModels\ServerBooksViewModel.cs) is such a candidate

NOTE that I could not open your project in VS for some reason, so I only scanned your files in notepad - there might be other such occurrences.

Check out Stephen Cleary's posts on deadlocks with async/await

G. Stoynev
  • 7,389
  • 6
  • 38
  • 49