The question was already answered in the comments. Thanks.
tl;dr version: I'm trying to make an async run synchronously according to this , but it's freezing.
I have
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
Which works fine.
I need to change it to not being async, so I tried changing it to what was mentioned in this answer with:
BitmapDecoder decoder = SyncDecoder(BitmapDecoder.CreateAsync(stream));
where:
BitmapDecoder SyncDecoder(IAsyncOperation<BitmapDecoder> async) { return Task.Run(() => async.AsTask()).Result; }
or a longer (I think equivalent) version:
BitmapDecoder SyncDecoder(IAsyncOperation<BitmapDecoder> async)
{
Func<Task<BitmapDecoder>> asLambda = () => async.AsTask();
Task<BitmapDecoder> asTask = Task.Run(asLambda);
asTask.Wait(); // freezes
return asTask.Result;
}
However, SyncDecoder
never returns. (In the 2nd version it freezes on Wait()
.)
I tried making the same method for IReadOnlyList<StorageFile>
(and storageFolder.GetFilesAsync()
) and it does work fine. So it should work here too. But doesn't.
I know the UI thread should generally not be blocked but we need to fix existing code. Not undertake a large refactoring project. So how do I transform the code to be synchronous?