public virtual void Trigger(PEventManager manager) { if (this.task != null) { throw new InvalidOperationException("This event can't be triggered before the previous lifecycle is finished."); } manager.SetupEventHandlers(this); if (this.Prepare != null) { try { Prepare(this as TEvent); } catch (Exception e) { TaskFail(e); return; } } if (this.Execute != null) { token = new CancellationTokenSource(); task = new Task((t) => this.Execute(this as TEvent), token); completeTask = task.ContinueWith(task => TaskComplete(task, manager)); task.Start(); } else { TaskComplete(null, manager); } }
internal void TaskComplete(Task t, PEventManager manager) { var ctrl = manager.MainThreadControl; if (ctrl != null && ctrl is ISynchronizeInvoke && (ctrl as ISynchronizeInvoke).InvokeRequired) { (ctrl as ISynchronizeInvoke).Invoke(new Action <Task>(task => TaskComplete(task, manager)), new object[] { t, manager }); } #if WPF else if (ctrl != null && ctrl is System.Windows.Threading.DispatcherObject && !(ctrl as System.Windows.Threading.DispatcherObject).Dispatcher.CheckAccess()) { (ctrl as System.Windows.Threading.DispatcherObject).Dispatcher.Invoke(new Action <Task>(task => TaskComplete(task, manager)), new object[] { t, manager }); } #endif else { try { this.IsSuccess = true; if (this.task != null && this.task.IsFaulted) { var exception = this.task.Exception.GetBaseException(); if (exception is PEventCancelException) { var canelException = exception as PEventCancelException; if (!canelException.IgnoreSuccess) { if (!canelException.Success) { this.IsSuccess = false; } } else { return; } } else { this.IsSuccess = false; } } if (this.IsSuccess) { if (this.Success != null) { this.Success(this as TEvent); } } else { if (this.Error != null) { this.Error(this as TEvent, this.task.Exception.GetBaseException()); } } } finally { this.task = null; this.completeTask = null; if (this.Complete != null) { this.Complete(this as TEvent); } } } }