Issue
I use term Task context -- as C# Task type -- and Android context to differentiate those two entities.
Better ask to be 100% sure -- when you work with WPF or WindowsForms you cannot interact with UI freely. For example when you use await
you want to preserve the Task context (so no usual ConfigureAwait(false)
).
So my question is -- when I try to send the intent from another thread do I have to make some preparation as well, say when I use await
do I have to preserve Task context? Or is act of sending preparation itself -- something like (far fetched) analogy to Dispatcher.Invoke
in WPF?
In other words can I just blindly send intent no matter what thread I am on, as long my Android context is valid? Or maybe it depends on the target thread -- when for example it is the main/UI one I should do something extra or it is guaranteed to work correctly always?
Solution
Assuming you have used the async and await pattern properly, then there should be no need to marshal back to the UI thread. It's the same as in winforms and WPF.
The exception to the rule is when you know you are NOT running on the UI context (i.e if you have used Task.Run
to offload to another thread etc.), in which case you would call the following to marshal back to the UI thread:
Device.BeginInvokeOnMainThread
Invokes an Action on the device main (UI) thread.
The above is Xamarin.Forms equivalent of Dispatcher.Invoke
.
Answered By - TheGeneral
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.