--- /dev/null
+using System.Threading;
+
+namespace Fitness
+{
+ /// <summary>
+ /// NUIContext captures SynchronizationContext.
+ /// </summary>
+ public static class NUIContext
+ {
+ private static SynchronizationContext context;
+
+ /// <summary>
+ /// Save SychronizationContext.
+ /// </summary>
+ /// <remarks>
+ /// Must be called from the main thread.
+ /// </remarks>
+ public static void Initialize()
+ {
+ context = SynchronizationContext.Current;
+
+ Services.Logger.Debug($"capturing SynchronizationContext: {context}");
+ }
+
+ /// <summary>
+ /// Invoke Action within captured SynchronizationContext.
+ /// </summary>
+ /// <param name="action">Action to be called on the main thread.</param>
+ public static void InvokeOnMainThread(System.Action action)
+ {
+ SendOrPostCallback callback = _ => action?.Invoke();
+ context?.Post(callback, null);
+ }
+ }
+}