/// <summary> /// Invokes an action in the GUI thread. /// </summary> public static Task InvokeAsync(Action action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } // Capture the current toolkit. It will be used in the invocation var targetToolkit = toolkit; var ts = new TaskCompletionSource <int> (); Action actionCall = () => { try { targetToolkit.InvokeAndThrow(action); ts.SetResult(0); } catch (Exception ex) { ts.SetException(ex); } }; if (UIThread == Thread.CurrentThread) { actionCall(); } else { engine.InvokeAsync(actionCall); } return(ts.Task); }
/// <summary> /// Invokes an action in the GUI thread /// </summary> /// <param name='action'> /// The action to execute. /// </param> public static void Invoke(Action action) { if (action == null) { throw new ArgumentNullException("action"); } engine.InvokeAsync(delegate { try { toolkit.EnterUserCode(); action(); toolkit.ExitUserCode(null); } catch (Exception ex) { toolkit.ExitUserCode(ex); } }); }