In .NET, Windows 8 and Windows Phone 7 I have code similar to this:
public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
{
if (
You can use SynchronizationContext.Post or Send method. It is portable, and when you use a UI framework with a dispatcher then the current synchronization context will delegate the work to the Dispatcher.
Specifically, you can use the following code:
void InvokeIfRequired(this SynchroniationContext context, Action action)
{
if (SynchroniationContext.Current == context)
{
action();
}
else
{
context.Send(action) // send = synchronously
// context.Post(action) - post is asynchronous.
}
}