问题
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