Asyn Task not working for downloading images.

shishirtiwarishishirtiwari shishir tiwariUSMember

I am trying to populate a listview with profile images from facebook friendlist using aync task but I am stuck at this point. Code is compiled but not responding once it enters the async method block and application hangs.
In my GetView method I have below code:

public override View GetView(int position, View convertView, ViewGroup parent)
{
            try
            {
                var item = Friends[position];

                var view = (convertView ??
                        context.LayoutInflater.Inflate(
                        Resource.Layout.FrindList,
                        parent,
                        false)) as LinearLayout;

                var profileImage = view.FindViewById<ImageView>(Resource.Id.imageViewProfilePicture);
                profileImage.SetImageBitmap(DownloadProfileImages(item.profileURL).Result);
                       return view;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

}

And here is the async method I am using:

public async Task<Bitmap> DownloadProfileImages(string url)
{
            try
            {
                WebClient webClient = new WebClient();
                byte[] bytes = null;

        // I am trying this for only 5 images, but once debugger pass below line , it never comes back again :(
        bytes = await webClient.DownloadDataTaskAsync(url);
                Bitmap bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
                return bitmap;
            }
            catch (Java.Lang.Exception ex)
            {
                throw ex;
            }

}

Please suggest me , whether am I doing something wrong here...

Answers

  • CheesebaronCheesebaron Tomasz Cielecki DKInsider, University mod

    You are calling .Result on your Task you are not running anything async....

  • shishirtiwarishishirtiwari shishir tiwari USMember
    edited September 2014

    Yes I found some where that I should do it like:
    profileImage.SetImageBitmap(await DownloadProfileImages(item.profileURL).Result);

    but it gives error await cannot be used outside the Async. May you please post any reference link or similar solution.

Sign In or Register to comment.