public static void BackgroundAction(object target, Action backgroundAction, Action befoe, Action callback, EndMode endMode, TaskImplMode taskMode) { var context = new BackgroundActionContext(endMode, () => { backgroundAction(); return(null); }); var container = target == null?backgroundAction.Target.As <IExtendedPresenter>() : target.As <IExtendedPresenter>(); var task = container.GetMetadata <IBackgroundThreadTask>(); if (task == null) { task = TaskImplFactory.CreateTask(taskMode); if (taskMode != TaskImplMode.UIThread) { container.WasShutdown += (s, e) => task.Dispose(); container.AddMetadata(task); } } //Before if (!befoe.IsNull()) { befoe(); } //Action task.Enqueue(context); //After if (!callback.IsNull()) { Application.Current.Dispatcher.BeginInvoke(callback, DispatcherPriority.ContextIdle); } }
/// <summary> /// Executes the specified this action on the specified target. /// </summary> /// <param name="actionMessage">The action message.</param> /// <param name="handlingNode">The node.</param> /// <param name="context">The context.</param> public override void Execute(ActionMessage actionMessage, IInteractionNode handlingNode, object context) { try { TryUpdateTrigger(actionMessage, handlingNode, true); var parameters = _messageBinder.DetermineParameters( actionMessage, _requirements, handlingNode, context ); var beforeProcessors = _filters.PreProcessors.OfType <IBeforeProcessor>(); foreach (var filter in _filters.PreProcessors.Except(beforeProcessors.Cast <IPreProcessor>())) { if (!filter.Execute(actionMessage, handlingNode, parameters)) { return; } } //Before foreach (var before in beforeProcessors.OrderByDescending(p => p.Priority)) { if (!before.BeforeExecute(actionMessage, handlingNode, parameters)) { return; } } CurrentBackgroundActionInfo = _filters.PostProcessors.OfType <IBackgroundActionInfo>().Single(); var container = handlingNode.MessageHandler.Unwrap() as IExtendedPresenter; CurrentBackgroundTask = container.GetMetadata <IBackgroundThreadTask>(); if (CurrentBackgroundTask == null) { CurrentBackgroundTask = TaskImplFactory.CreateTask(CurrentBackgroundActionInfo.TaskMode); container.WasShutdown += (s, e) => CurrentBackgroundTask.Dispose(); container.AddMetadata(CurrentBackgroundTask); AttachTaskEvents(actionMessage, handlingNode, parameters); } //Exectue Before method if (CurrentBackgroundActionInfo.BeforeMethod != null) { var result = CurrentBackgroundActionInfo.BeforeMethod.Invoke(container, parameters); bool canProceed = true; if (CurrentBackgroundActionInfo.BeforeMethod.Info.ReturnType == typeof(bool)) { canProceed = result.As <bool>(); } if (!canProceed) { return; } } DoExecute(actionMessage, handlingNode, parameters); } catch (Exception ex) { TryUpdateTrigger(actionMessage, handlingNode, false); if (!TryApplyRescue(actionMessage, handlingNode, ex)) { throw; } OnCompleted(); } }