Stopbyte

My WinRT App Hangs forever at StorageFile.GetThumbnailAsync()

I am trying to load Thumbnails for a list of (Video, Audio and Image files), but my WinRT app keeps hanging, i am using the IValueConverter Below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
 
namespace App1.Common
{
    public class ContentPathConverter : IValueConverter
    {
        private async Task<object> x(object value)
        {
            string s = value as string;
            try
            {
                Uri uri = new Uri(s, UriKind.RelativeOrAbsolute);
                var mediaFile = await StorageFile.GetFileFromPathAsync(s);
                var thumbnail = await mediaFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView);
                BitmapImage img = new BitmapImage();
                img.SetSource(thumbnail);
                return img;
            }
            catch
            {
                return null;
            }
        }
 
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return x(value).Result;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}

it Keeps hanging every time at this line:

var thumbnail = await mediaFile.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView);

How can i solve this?

thanks.

2 Likes

I believe you should know that it’s not recommended to use IValueConverter that why, with async and Await.

Await in the IValueConverter causes the UI thread to freeze, and usually causes dead locks. it wont work that way,

I highly recommend using some MVVM patter, and put that code in a ViewModel.

2 Likes

As @Yassine posted, it’s not advisable to use IValueConverter when await is used, as that can cause for the UI thread to freeze. And i must add you should not use IValueConverter at all in a WinRT app. Instead, you may use an MVVM pattern in your Apps, and load, Convert…and do all data manipulation stuff in your ViewModels or Models. that can improve your App’s Performance while reducing Complexity.

1 Like