UnoPlatform -The application called an interface that was marshalled for a different thread

1

i am using Uno prism template for my Uno platform App. After making an async call in UWP app. i.e.

var content = await GetHttpContentWithTokenAsync(graphAPIEndpoint,authResult.AccessToken).ConfigureAwait(false);

whenever RaisePropertyChanged() method hits I am getting following error=>
{"The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"}

Git repo=> https://github.com/avikeid2007/Repayabl

File=> https://github.com/avikeid2007/Repayabl/blob/dev/RepayablClient/RepayablClient.Shared/ViewModels/LoginViewModel.cs#L131

prism
uno-platform
asked on Stack Overflow May 30, 2020 by Avnish kumar • edited May 30, 2020 by Avnish kumar

1 Answer

3

As mentioned in the comment, the problem here is the use of .ConfigureAwait(false).

With ConfigureAwait(false) you're telling the compiler, 'after this awaited method executes, don't bother coming back to the current context, I'm good.' In some cases this can be an appropriate optimization, or even a guard against deadlocking, particularly when you're writing a class library.

However you normally shouldn't use it in callbacks from the UI thread, like this case. In UWP and indeed most UI frameworks, you can only modify the UI from the main UI thread, otherwise you will get exactly the error you mention. So you need to be returned to the same context after the awaited method completes.

TLDR: Remove ConfigureAwait(false), don't use it in async methods invoked from the UI thread.

answered on Stack Overflow Jun 1, 2020 by David Oliver

User contributions licensed under CC BY-SA 3.0