Make Execute.OnUIThread synchronous, with BeginOnUIThread being the async version

This commit is contained in:
Antony Male 2014-04-23 21:19:50 +01:00
parent 8cc0ce1ba9
commit 7cc888d090
1 changed files with 27 additions and 1 deletions

View File

@ -17,7 +17,7 @@ namespace Stylet
public static Action<Action> DefaultPropertyChangedDispatcher = Execute.OnUIThread;
public static void OnUIThread(Action action)
public static void BeginOnUIThread(Action action)
{
// If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously
if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current)
@ -26,6 +26,32 @@ namespace Stylet
action();
}
public static void OnUIThread(Action action)
{
Exception exception = null;
if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current)
{
SynchronizationContext.Send(_ =>
{
try
{
action();
}
catch (Exception e)
{
exception = e;
}
}, null);
if (exception != null)
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
}
else
{
action();
}
}
public static Task OnUIThreadAsync(Action action)
{
// If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously