The docs state to call all methods of FilePicker on the ui-thread, so I made the following call:
await MainThread.InvokeOnMainThreadAsync(async () =>
{
IEnumerable<FileResult> images = await FilePicker.PickMultipleAsync(new PickOptions()
{
FileTypes = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.WinUI, [".jpg", ".png", ".jpeg", ".bmp", ".tif", ".tiff"] }
})
});
... // some code to parse the images
});
The windows explorer opens and I can choose multiple images, but as soon as I click "open" or "cancel" PickMultipleAsync throws a System.Runtime.InteropServices.COMException with the message The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD)).
Anyone having any ideas?
Edit: What i've forgot to mention is that the problem only appears sometimes.
COMExceptionthrown on theFilePicker.PickMultipleAsyncline exactly?InvokeOnMainThreadAsync"just to be safe" sounds like a poor practice to me. It results in more complicated code for no good reason, and could in worst case result in deadlocks. If it is designed to run on the UI thread it is better to trow an error if it is not, so you can fix the problem.