mirror of https://github.com/AMT-Cheif/Stylet.git
Make Execute.OnUIThread synchronous, with BeginOnUIThread being the async version
This commit is contained in:
parent
8cc0ce1ba9
commit
7cc888d090
|
@ -17,7 +17,7 @@ namespace Stylet
|
||||||
|
|
||||||
public static Action<Action> DefaultPropertyChangedDispatcher = Execute.OnUIThread;
|
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 we're already on the given SynchronizationContext, or it hasn't been set, run synchronously
|
||||||
if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current)
|
if (SynchronizationContext != null && SynchronizationContext != SynchronizationContext.Current)
|
||||||
|
@ -26,6 +26,32 @@ namespace Stylet
|
||||||
action();
|
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)
|
public static Task OnUIThreadAsync(Action action)
|
||||||
{
|
{
|
||||||
// If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously
|
// If we're already on the given SynchronizationContext, or it hasn't been set, run synchronously
|
||||||
|
|
Loading…
Reference in New Issue