Can't Get WindowsApiCodePack ShellFile Thumbnail Async

大憨熊 提交于 2020-01-04 17:39:02

问题


I am trying to get the thumbnails for files in a directory asynchronously. All files except PDFs seem to work async.

if (System.IO.File.Exists(filePath))
{
    var task = await Task.Factory.StartNew(async () =>
    { 
         using(ShellFile shellFile = ShellFile.FromFilePath(filePath))
         {
             ImageSource source = shellFile.Thumbnail.MediumBitmapSource;
             source.Freeze();
             return source;
         }
    });
    image.Dispatcher.Invoke(() => image.Source = task.Result);
}

All other files return correctly. However, if I call all of this code a second time if image.source == null then it works fine.

edit My working code after Hans Passant's answer

var thread = new Thread(() =>
{
    using(ShellFile shellFile = ShellFile.FromFilePath(filePath))
    {
         ImageSource source = shellFile.Thumbnail.MediumBitmapSource;
         source.Freeze();
    }
    image.Dispatcher.Invoke(() => image.Source = source);
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Thanks!

来源:https://stackoverflow.com/questions/28033429/cant-get-windowsapicodepack-shellfile-thumbnail-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!