注意:如果您需要完整的源代码,此问题中的代码是deSleeper的一部分.
我想要的命令之一是异步操作的烘焙设计.我希望在执行命令时按下按钮以禁用,并在完成时返回.我希望在ThreadPool工作项中执行实际工作.最后,我想要一种方法来处理异步处理过程中发生的任何错误.
我的解决方案是AsyncCommand:
public abstract class AsyncCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public event EventHandler ExecutionStarting;
public event EventHandler<AsyncCommandCompleteEventArgs> ExecutionComplete;
public abstract string Text { get; }
private bool _isExecuting;
public bool IsExecuting
{
get { return _isExecuting; }
private set
{
_isExecuting = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
protected abstract void OnExecute(object parameter);
public void Execute(object parameter)
{
try
{
IsExecuting = true;
if (ExecutionStarting != null)
ExecutionStarting(this, EventArgs.Empty);
var …Run Code Online (Sandbox Code Playgroud)