public void Execute(ActionExecutionContext context) { Exception error = null; var worker = new BackgroundWorker(); worker.DoWork += (s, e) => { try { _work(); } catch (Exception ex) { error = ex; } }; worker.RunWorkerCompleted += (s, e) => { if (error == null && _onSuccess != null) { _onSuccess.OnUIThread(); } if (error != null && _onFail != null) { Caliburn.Micro.Execute.OnUIThread(() => _onFail(error)); } Completed(this, new ResultCompletionEventArgs { Error = error }); }; worker.RunWorkerAsync(); }
public void ExecuteAsync() { Exception error = null; _worker.DoWork += (_, __) => { try { _work(); } catch (Exception ex) { error = ex; } }; _worker.RunWorkerCompleted += (s, e) => { if (error == null && _onSuccess != null) { _onSuccess?.OnUIThread(); } if (error != null && _onFail != null) { Execute.OnUIThread(() => _onFail?.Invoke(error)); } }; _worker.RunWorkerAsync(); }
public static bool TryOnUiThread(Action action) { try { action.OnUIThread(); return true; } catch (AggregateException e) { var rethrow = true; e.Handle(x => { if (x is OperationCanceledException) rethrow = false; return true; }); if (rethrow) throw; return false; } }
public static bool TryOnUiThread(Action action) { try { action.OnUIThread(); return(true); } catch (AggregateException e) { var rethrow = true; e.Handle(x => { if (x is OperationCanceledException) { rethrow = false; } return(true); }); if (rethrow) { throw; } return(false); } }
/// <summary> /// Executes the given action on the UI thread /// </summary> /// <remarks>An extension point for subclasses to customise how property change notifications are handled.</remarks> /// <param name="action"></param> protected virtual void OnUIThread(System.Action action) => action.OnUIThread();
private void SyncAppBar(DependencyObject view) { if (view == null || _page == null) { return; } // Reset waiting flag if (_waiting) { _waiting = false; } // Get the last visible BindableAppBar in the tree var lastVisibleAppBar = view.GetVisualDescendants() .OfType <BindableAppBar>() .LastOrDefault(a => a.IsVisible); // Show the last appbar that's visible, since having two visible // appbars doesn't make sense in a view; but one could be visible // and the other invisible (hot swapping!) if (lastVisibleAppBar != null) { System.Action updateAppBar = () => { // Refresh button state/bg color if the appbar was "hidden" lastVisibleAppBar.Invalidate(); // Assign the bar _page.ApplicationBar = lastVisibleAppBar.ApplicationBar; }; // In Panorama, changing appbar visibility causes the transition to stop // so we handle if there was no appbar previously shown if (_page.ApplicationBar == null && _panorama != null) { _waiting = true; // Animations take ~500ms ThreadPool.QueueUserWorkItem(c => { Thread.Sleep(PanoramaWaitThreshold); // Are we still waiting on this? if (_waiting) { updateAppBar.OnUIThread(); } }); } else { // Update it updateAppBar(); } } else { // In Panorama, changing appbar visibility causes the transition to stop if (_panorama != null) { // These properties are safe to change and do not cause the transition // to abort if (_page.ApplicationBar != null) { _page.ApplicationBar.Buttons.Clear(); _page.ApplicationBar.MenuItems.Clear(); _page.ApplicationBar.BackgroundColor = Color.FromArgb(1, 0, 0, 0); } } else { _page.ApplicationBar = null; } } }