在我的应用程序中,我有一个命令,我只希望用户能够在它尚未运行时触发。有问题的命令绑定到 WPF 按钮,这意味着如果 CanExecute 为 false,它会自动禁用该按钮。到现在为止还挺好。
不幸的是,该命令执行的操作是一个长时间运行的操作,因此它需要发生在不同的线程上。我不认为这会是一个问题......但它似乎是。
我已经提取了一个可以显示问题的最小样本。如果绑定到一个按钮(通过 LocalCommands.Problem 静态引用),该按钮将根据需要被禁用。当工作线程尝试更新 CanExecute 时,将从 System.Windows.Controls.Primitives.ButtonBase 内部抛出 InvalidOperationException。
解决此问题的最合适方法是什么?
示例命令代码如下:
using System;
using System.Threading;
using System.Windows.Input;
namespace InvalidOperationDemo
{
static class LocalCommands
{
public static ProblemCommand Problem = new ProblemCommand();
}
class ProblemCommand : ICommand
{
private bool currentlyRunning = false;
private AutoResetEvent synchronize = new AutoResetEvent(false);
public bool CanExecute(object parameter)
{
return !CurrentlyRunning;
}
public void Execute(object parameter)
{
CurrentlyRunning = true;
ThreadPool.QueueUserWorkItem(ShowProblem);
}
private void ShowProblem(object state)
{
// Do some work here. When we're done, set CurrentlyRunning back to false.
// To simulate the problem, wait on the never-set synchronization object.
synchronize.WaitOne(500);
CurrentlyRunning = false;
}
public bool CurrentlyRunning
{
get { return currentlyRunning; }
private set
{
if (currentlyRunning == value) return;
currentlyRunning = value;
var onCanExecuteChanged = CanExecuteChanged;
if (onCanExecuteChanged != null)
{
try
{
onCanExecuteChanged(this, EventArgs.Empty);
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.Message, "Exception in event handling.");
}
}
}
}
public event EventHandler CanExecuteChanged;
}
}