From e37068838adf7726dcb9ea0c09e89e588b275e31 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sun, 23 Feb 2014 20:05:56 +0000 Subject: [PATCH] Execute runs delegate synchronously if possible --- Stylet/Execute.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Stylet/Execute.cs b/Stylet/Execute.cs index 0a7f059..75fb0f4 100644 --- a/Stylet/Execute.cs +++ b/Stylet/Execute.cs @@ -13,7 +13,8 @@ namespace Stylet public static void OnUIThread(Action action) { - if (SynchronizationContext != null) + // If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously + if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current) SynchronizationContext.Post(_ => action(), null); else action(); @@ -21,7 +22,8 @@ namespace Stylet public static Task OnUIThreadAsync(Action action) { - if (SynchronizationContext != null) + // If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously + if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current) { var tcs = new TaskCompletionSource(); SynchronizationContext.Post(_ =>