public virtual void DoExecute(object param) { // Invoke the executing command, allowing the command to be cancelled. CancelCommandEventArgs args = new CancelCommandEventArgs() { Parameter = param, Cancel = false }; InvokeExecuting(args); // If the event has been cancelled, bail now. if (args.Cancel) { return; } // Call the action or the parameterized action, whichever has been set. InvokeAction(param); // Call the executed function. InvokeExecuted(new CommandEventArgs() { Parameter = param }); }
protected void InvokeExecuting(CancelCommandEventArgs args) { CancelCommandEventHandler executing = Executing; // Call the executed event. if (executing != null) { executing(this, args); } }
/// <summary> /// Executes the command. /// </summary> /// <param name="param">The param.</param> public override void DoExecute(object param) { // If we are already executing, do not continue. if (IsExecuting) { return; } // Invoke the executing command, allowing the command to be cancelled. CancelCommandEventArgs args = new CancelCommandEventArgs() { Parameter = param, Cancel = false }; InvokeExecuting(args); // If the event has been cancelled, bail now. if (args.Cancel) { return; } // We are executing. IsExecuting = true; // Store the calling dispatcher. #if !SILVERLIGHT callingDispatcher = Dispatcher.CurrentDispatcher; #else callingDispatcher = System.Windows.Application.Current.RootVisual.Dispatcher; #endif // Run the action on a new thread from the thread pool (this will therefore work in SL and WP7 as well). ThreadPool.QueueUserWorkItem( (state) => { // Invoke the action. InvokeAction(param); // Fire the executed event and set the executing state. ReportProgress( () => { // We are no longer executing. IsExecuting = false; // If we were cancelled, invoke the cancelled event - otherwise invoke executed. if (IsCancellationRequested) { InvokeCancelled(new CommandEventArgs() { Parameter = param }); } else { InvokeExecuted(new CommandEventArgs() { Parameter = param }); } // We are no longer requesting cancellation. IsCancellationRequested = false; } ); } ); }